美文网首页
点击状态栏,列表滑动到顶部

点击状态栏,列表滑动到顶部

作者: rlqs | 来源:发表于2016-07-29 17:59 被阅读34次

    //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

    相关文章

      网友评论

          本文标题:点击状态栏,列表滑动到顶部

          本文链接:https://www.haomeiwen.com/subject/fddcjttx.html