先说简单的方法:
if (_sourceViewController.isViewLoaded && _sourceViewController.view.window) {
NSLog(@"屏幕上");
isCurrentVC = YES;
}
再说非常麻烦的方法
//获取当前屏幕显示的viewcontroller
- (UIViewController *)getCurrentVC
{
UIViewController *result = nil;
UIWindow * window = [[UIApplication sharedApplication] keyWindow];
if (window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows];
for(UIWindow * tmpWin in windows)
{
if (tmpWin.windowLevel == UIWindowLevelNormal)
{
window = tmpWin;
break;
}
}
}
UIView *frontView = [[window subviews] objectAtIndex:0];
id nextResponder = [frontView nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
result = nextResponder;
else
result = window.rootViewController;
return result;
}
- (BOOL)isCurrentDisplayViewControllerIsFollowVC{
BOOL isCurrentVC = NO;
UIViewController *VC = [self getCurrentVC];
if ([VC isKindOfClass:[UINavigationController class]]) {
UINavigationController *naviVC = (UINavigationController *)VC;
UIViewController *currentVC = naviVC.topViewController;
if ([currentVC isKindOfClass:[PDRootViewController class]]) {
NSLog(@"PDRootViewController");
PDRootViewController *pdRootVC = (PDRootViewController *)currentVC;
NSLog(@"PDRootViewController childViewControllers is %@", pdRootVC.childViewControllers);
NSLog(@"the _sourceViewController is %@", _sourceViewController);
for (UIView *subView in pdRootVC.view.subviews) {
if ([subView isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView *)subView;
if (scrollView.contentOffset.x < scrollView.bounds.size.width){
NSLog(@" 关注页面 ");
for (UIView *subView in scrollView.subviews) {
if ([subView isEqual:_sourceViewController.view]) {
isCurrentVC = YES;
NSLog(@" 关注....");
}
}
}
}
}
}
NSLog(@"the current VC is %@", currentVC);
}
return isCurrentVC;
}