UITableVIewiOS技能收藏控件封装

ios-自定义点击状态栏滚回顶部

2015-12-30  本文已影响2179人  没阳光的午后

点击状态栏滚回顶部这个功能是系统自带的,只需要设置self.scrollView.scrollsToTop = YES即可,但是这个属性有一个前提是窗口下必须只有一个可滚动的View才有效果,这时候就需要自定义创建一个窗口来完成这个功能

添加窗口

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        topWindow_ = [[UIWindow alloc] init];
        topWindow_.windowLevel = UIWindowLevelAlert;
        topWindow_.frame = [UIApplication sharedApplication].statusBarFrame;
        topWindow_.backgroundColor = [UIColor clearColor];
        topWindow_.hidden = NO;
        [topWindow_ addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topWindowClick)]];
    });

在监听方法中遍历子控件,如果是scrollView就滚回顶部

    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    for (UIView *subview in view.subviews) {
        [self searchAllScrollViewsInView:subview];
    }
    
    if (![view isKindOfClass:[UIScrollView class]]) return;
    
    // 找到了UIScrollView
    UIScrollView *scrollView = (UIScrollView *)view;
    [scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];

但是这样会有一个BUG,如果屏幕旋转的话,添加的Viewframe会不正确,接下来解决这个BUG

topWindow_.rootViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth;

上面代码还有个BUG是点击状态栏会将所有的ScrollView全部滚回顶部,这里需要判断当前View是否与窗口有重叠,重叠才滚回顶部


- (BOOL)bs_intersectsWithAnotherView:(UIView *)anotherView
{
    if (anotherView == nil) anotherView = [UIApplication sharedApplication].keyWindow;
    
    // 判断self和anotherView是否重叠
    CGRect selfRect = [self convertRect:self.bounds toView:nil];
    CGRect anotherRect = [anotherView convertRect:anotherView.bounds toView:nil];
    return CGRectIntersectsRect(selfRect, anotherRect);
}

以上方法可以判断不是同一坐标系的2个View是否重叠,并且如果anotherView为空的话,就返回窗口

if (![scrollView bs_intersectsWithAnotherView:nil]) return

在监听方法中加入以上代码即可完成功能

上一篇下一篇

猜你喜欢

热点阅读