需求:点击顶部statusBar,scrollView滚动到最顶部
当控制器上面只有一个UIScrollView的时候,系统才会为这个UIScrollView添加这个自动回滚到顶部的功能
当一个控制器上有多个UIScrollView时,系统就懵了,因为他不确定到底要为那个UIScrollView设置这个自动回滚到顶部的功能,这个时候就只能有我们coder明确的告诉系统:我们要为哪个UIScrollView设置自动回滚到顶部的功能
我们只为显示在用户眼前的UIScrollView设置那个功能
- 1.自定义一个类,继承自NSObject
#import <Foundation/Foundation.h>
@interface DXTopWindow : NSObject
+ (void)show;
@end
- 2.对外提供的show方法
// 声明一个全局变量
static UIWindow *window_;
+ (void)show{
// 延迟加载窗口,保证这个窗口在所有窗口的最上面
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
window_ = [[UIWindow alloc] init];
window_.frame = [UIApplication sharedApplication].statusBarFrame;
//修正frame,避免遮挡
window_.frame=CGRectMake(100+window_.frame.origin.x, window_.frame.origin.y, window_.frame.size.width, window_.frame.size.height);
// 窗口默认显示黑色
window_.backgroundColor = [UIColor clearColor];//点击事件可以获取??
// UIWindowLevelAlert > UIWindowLevelStatusBar > UIWindowLevelNormal
// 窗口默认等级是UIWindowLevelNormal,等级最低,放在最下层,点击手势被拦截,无法被监听到
window_.windowLevel = UIWindowLevelAlert;
// 默认是隐藏的
window_.hidden = NO;
[window_ addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topWindowClick)]];
// [window_ makeKeyAndVisible];
});
}
+ (void)topWindowClick{
// 主窗口
UIWindow *window = [UIApplication sharedApplication].keyWindow;
// 查找主窗口中的所有scrollView
[self findScrollViewsInView:window];
}
- 3.使用递归查找所以子控件
+ (void)findScrollViewsInView:(UIView *)view{
// 窗口也继承UIView
// 利用递归查找所有的子控件
for (UIView *subview in view.subviews) {
[self findScrollViewsInView:subview];
}
if (![view isKindOfClass:[UIScrollView class]]) return;
// 判断是否跟window有重叠
if (![view intersectWithView:[UIApplication sharedApplication].keyWindow]) return;
// CGRect windowRect = [UIApplication sharedApplication].keyWindow.bounds;
// CGRect viewRect = [view convertRect:view.bounds toView:nil];
// // 跟window不重叠
// if (!CGRectIntersectsRect(windowRect, viewRect)) return;
// 如果是scrollView
UIScrollView *scrollView = (UIScrollView *)view;
// 修改offset
CGPoint offset = scrollView.contentOffset;
offset.y = - scrollView.contentInset.top;
[scrollView setContentOffset:offset animated:YES];
// [scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
}
给UIView扩展的分类方法
// 判断两个View是否相交
- (BOOL)intersectWithView:(UIView *)view{
UIWindow *window = [UIApplication sharedApplication].keyWindow;
CGRect selfRect = [self convertRect:self.bounds toView:window];
CGRect viewRect = [view convertRect:view.bounds toView:window];
return CGRectIntersectsRect(selfRect, viewRect);
}
使用了递归输出所有子控件应用示例
extension UIView {
// 使用了递归输出所有子控件
public class func dx_foundView(view: UIView, className: String) -> UIView? {
if let tmpClass = NSClassFromString(className) {
// 递归出口
if view.isKind(of: tmpClass) {
return view
}
for subView in view.subviews {
let foundView = dx_foundView(view: subView, className: className)
if foundView != nil {
return foundView
}
}
}
return nil
}
}