//XMGTopWindow.m
#import"XMGTopWindow.h"
@implementationXMGTopWindow
staticUIWindow*window_;
+ (void)initialize
{
window_= [[UIWindowalloc]init];
window_.frame=CGRectMake(0,0,XMGScreenW,20);
window_.windowLevel=UIWindowLevelAlert;
[window_addGestureRecognizer:[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(windowClick)]];
}
+ (void)show
{
window_.hidden=NO;
}
+ (void)hide
{
window_.hidden=YES;
}
/**
*监听窗口点击
*/
+ (void)windowClick
{
UIWindow*window = [UIApplicationsharedApplication].keyWindow;
[selfsearchScrollViewInView:window];
}
+ (void)searchScrollViewInView:(UIView*)superview
{
for(UIScrollView*subviewinsuperview.subviews) {
//如果是scrollview,滚动最顶部
if([subviewisKindOfClass:[UIScrollViewclass]] && subview.isShowingOnKeyWindow) {
CGPointoffset = subview.contentOffset;
offset.y= - subview.contentInset.top;
[subviewsetContentOffset:offsetanimated:YES];
}
//继续查找子控件
[selfsearchScrollViewInView:subview];
}
}
@end
#import"UIView+XMGExtension.h"
@implementationUIView (XMGExtension)
- (BOOL)isShowingOnKeyWindow
{
//主窗口
UIWindow*keyWindow = [UIApplication sharedApplication].keyWindow;
//以主窗口左上角为坐标原点,计算self的矩形框
CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
CGRect winBounds = keyWindow.bounds;
//主窗口的bounds和self的矩形框是否有重叠
BOOL intersects =CGRectIntersectsRect(newFrame, winBounds);
return!self.isHidden&&self.alpha>0.01&&self.window== keyWindow && intersects;
}
@end