點擊狀態(tài)欄UIScrollView回滾到頂部

從iOS10開始,蘋果官方添加點擊狀態(tài)欄,讓UIScrollView回滾到頂部,但是iOS9之前的版本需要程序員寫代碼實現(xiàn)該功能。

95A3F73A9269AFF74534C3ABAEEF919F.jpg

實現(xiàn)思路


1,狀態(tài)欄處添加一個UIWindow

創(chuàng)建一個UIWindow對象,并添加點擊手勢,而且windowLevel大于UIWindowLevelStatusBar

+ (void)initialize
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    window_ = [[UIWindow alloc] init];
    window_.windowLevel = UIWindowLevelAlert;
    window_.frame = [UIApplication sharedApplication].statusBarFrame;
      
    window_.x = 0;
    window_.width = window_.width - window_.x;
    window_.backgroundColor = [UIColor clearColor];
    window_.hidden = NO;
    [window_ addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(windowClick)]];
        
    // 添加通知刷新
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowClick) name:FOFTopWindowNeedClickNotification object:nil];;
    });
}

對外提供show和hide類方法

+ (void)show
{
    
    window_.hidden = NO;
}

+ (void)hide
{
    window_.hidden = YES;
}

2,遞歸尋找展示在keyWindow上的UIScrollView控件

+ (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];
        }
        
        // 繼續(xù)查找子控件
        [self searchScrollViewInView:subview];
    }
}

3,判斷該scrollView是否和keyWindow重疊(難點)

- (BOOL)isShowingOnKeyWindow
{
    // 主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    // 以主窗口左上角為坐標(biāo)原點, 計算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;
}

利用UIView對象方法轉(zhuǎn)換坐標(biāo)系

- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;

4,在AppDelegate添加show方法

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    
    // 添加一個window, 點擊這個window, 可以讓屏幕上的scrollView滾到最頂部
    [FOFTopWindow show];
}

** Demo代碼查看git **
https://github.com/tangbin583085/TBTopWindow

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容