Swift版
import UIKit
class TopWindow: UIWindow {
private static let window_: UIWindow = UIWindow()
/// 类初始化方法,保证window_只被创建一次
override class func initialize() {
window_.frame = CGRectMake(0, 0, global.appWidth, 20)
window_.windowLevel = UIWindowLevelAlert
window_.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "topWindowClick"))
}
class func topWindowClick() {
// 遍历当前主窗口所有view,将满足条件的scrollView滚动回原位
searchAllowScrollViewInView(UIApplication.sharedApplication().keyWindow!)
}
private class func searchAllowScrollViewInView(superView: UIView) {
for subview: UIView in superView.subviews as! [UIView] {
if subview.isKindOfClass(UIScrollView.self) && superView.viewIsInKeyWindow() {
// 拿到scrollView的contentOffset
var offest = (subview as! UIScrollView).contentOffset
// 将offest的y轴还原成最开始的值
offest.y = -(subview as! UIScrollView).contentInset.top
// 重新设置scrollView的内容
(subview as! UIScrollView).setContentOffset(offest, animated: true)
}
// 递归,让子控件再次调用这个方法判断时候还有满足条件的view
searchAllowScrollViewInView(subview)
}
}
/// 添加topWindow,使手势生效
class func showTopWindow() {
window_.hidden = false
}
/// 隐藏topWindow,移除手势
class func hiddenTopWindow() {
window_.hidden = true
}
}
/// 对UIView的一个扩展
extension UIView {
/// 判断调用方法的view是否在keyWindow中
func viewIsInKeyWindow() -> Bool {
let keyWindow = UIApplication.sharedApplication().keyWindow!
// 将当前view的坐标系转换到window.bounds
let viewNewFrame = keyWindow.convertRect(self.frame, fromView: self.superview)
let keyWindowBounds = keyWindow.bounds
// 判断当前view是否在keyWindow的范围内
let isIntersects = CGRectIntersectsRect(viewNewFrame, keyWindowBounds)
// 判断是否满足所有条件
return !self.hidden && self.alpha > 0.01 && self.window == keyWindow && isIntersects
}
}
在需要的视图控制器中调用TopWindow.showTopWindow(),视图将要消失的时候调用TopWindow.hiddenTopWindow()
Objective-C版
.h文件
#import @interface ZGJTopWindow : NSObject
+ (void)show;
+ (void)hide;
@end
.m文件
#import "ZGJTopWindow.h"
@implementation ZGJTopWindow
static UIWindow *window_;
//初始化window
+ (void)initialize {
window_ = [[UIWindow alloc] init];
window_.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20);
window_.windowLevel = UIWindowLevelAlert;
[window_ addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(windowClick)]];
}
+ (void)show {
window_.hidden = NO;
}
+ (void)hide {
window_.hidden = YES;
}
// 监听窗口点击
+ (void)windowClick {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[self searchScrollViewInView:window];
}
+ (void)searchScrollViewInView:(UIView *)superview {
for (UIScrollView *subview in superview.subviews) {
// 如果是scrollview, 滚动最顶部
if ([subview isKindOfClass:[UIScrollView class]] && [subview isShowingOnKeyWindow]) {
CGPoint offset = subview.contentOffset;
offset.y = - subview.contentInset.top;
[subview setContentOffset:offset animated:YES];
}
// 递归继续查找子控件
[self searchScrollViewInView:subview];
}
}
+ (BOOL)isShowingOnKeyWindow:(UIView *)view {
// 主窗口
UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
// 以主窗口左上角为坐标原点, 计算self的矩形框
CGRect newFrame = [keyWindow convertRect:view.frame fromView:view.superview];
CGRect winBounds = keyWindow.bounds;
// 主窗口的bounds 和 self的矩形框 是否有重叠
BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
return !view.isHidden && view.alpha > 0.01 && view.window == keyWindow && intersects;
}
@end