@interface AppDelegate ()
@end
@implementation AppDelegate
UIWindow * topWindow;//设置一个全局变量
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
//这里把topWindow设置在状态栏的位置。并设置放置最外层。加入一个手势。
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
topWindow = [[UIWindow alloc]init];
topWindow.frame = [UIApplication sharedApplication].statusBarFrame;
topWindow.backgroundColor = [UIColor redColor];
topWindow.windowLevel = UIWindowLevelAlert;
topWindow.hidden = NO;
[topWindow addGestureRecognizer:[[UIGestureRecognizer alloc] initWithTarget:self action:@selector(topWindowClick)]];
});
return YES;
}
- (void)topWindowClick{
UIWindow * window = [UIApplication sharedApplication].keyWindow;
[self findScrollViewsInView:window];
}
- (void)findScrollViewsInView:(UIView *)view{
for (UIView * subView in view.subviews) {//用个递归找到scrollView;
[self findScrollViewsInView:subView];
}
if (![view isKindOfClass:[UIScrollView class]]) return;
UIView * windowView = [UIApplication sharedApplication].keyWindow;
BOOL interSect = CGRectIntersectsRect(windowView.bounds,[windowView convertRect:view.bounds fromView:nil] );
if (!interSect) return;//如果有多个页面。这里判断拿到的页面是否是当前显示页面。如果是则滚动到顶部,否则跳过。
UIScrollView * scrollView = (UIScrollView *)view;
[scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
}
***************************以下方法用于判断一个视图是否与当前视图重叠(该方法建议放在UIView的扩展中)**********************
- (BOOL)keyWindowinterSectWithView:(UIView *)view{
UIWindow * window = [UIApplication sharedApplication].keyWindow;
CGRect selfRect = [self convertRect:self.bounds fromView:window];
CGRect viewRect = [view convertRect:view.bounds toView:window];
return CGRectIntersectsRect(selfRect, viewRect);
}