点击状态栏返回

//
//  HQWindowBack.m
//  FunnySister
//
//  Created by hq on 16/5/5.
//  Copyright © 2016年 hanqing. All rights reserved.
//

#import "HQWindowBack.h"

@implementation HQWindowBack

//+不能访问属性,因此我们使用全局变量
static UIWindow *window_;

+(void)initialize{

    UIViewController* vc = [[UIViewController alloc]initWithNibName:nil bundle:nil];
    
    window_=[[UIWindow alloc] init];
    
    window_.frame=CGRectMake(0, 0, HQScreenWidth, 20);
    
    window_.windowLevel=UIWindowLevelStatusBar;
    
    window_.backgroundColor=[UIColor clearColor];
    
    //必须设置一个rootView,否则会崩溃
    window_.rootViewController=vc;
    
    //为window添加手势,点击之后返回顶部
    [window_ addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollToTop)]];
}


+(void) scrollToTop{
   
    UIWindow *keyWindow=[UIApplication sharedApplication].keyWindow;
    
    [self searchScrollview:keyWindow];
    
}


+(void) searchScrollview:(UIView *) superView{
    
    //获取该窗口下的所有scrollview
    for (UIView *uiView in superView.subviews) {
        
        if ([uiView isKindOfClass:[UIScrollView class]]) {
            
            UIScrollView *scroView=(UIScrollView *) uiView;
            
            //转换坐标系(将scrollview的坐标系转换到我们的当前窗口)
            UIWindow *win=[UIApplication sharedApplication].keyWindow;
            
            //将scroview从他的父控件的坐标转换到我们的窗口
            CGRect newFrame=[win convertRect:scroView.frame fromView:scroView.superview];
            
            //判断当前scrollview是否在我们的窗口内
            BOOL isInWindow=CGRectIntersectsRect(win.bounds, newFrame);
            
            //只改动当前窗口的
            BOOL isShowing=!scroView.hidden&&scroView.alpha>0.01&&isInWindow&&scroView.window==win;
            
            if (isShowing) {
               
                CGPoint p=scroView.contentOffset;

                p.y=0;
                
                [scroView setContentOffset:p animated:YES];
                
            }
        }
        
        [self searchScrollview:uiView];
    }
}


+(void)show{

    window_.hidden=NO;
}



@end
 
  
 
  

点击状态栏返回_第1张图片


你可能感兴趣的:(点击状态栏返回)