iOS開發 獲取狀態欄的點擊事件


首先我們追蹤UIStatusBar的觸摸事件,需要在AppDelegate里面加入以下代碼

復制代碼
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    CGPoint location = [[[event allTouches] anyObject] locationInView:self.window];
    CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
    if (CGRectContainsPoint(statusBarFrame, location)) {
        [self statusBarTouchedAction];
    }
}
復制代碼

然后在statusBarTouchedAction方法中將顯示在當前keyWindow里面的scrollView滾動到頂部

- (void)statusBarTouchedAction {

      [[DDTopWindow defaultDDTopWindow] scrollsToTop];

}

下面來看DDTopWindow

復制代碼

@interface DDTopWindow : UIView

 

+ (instancetype)defaultDDTopWindow;

 

- (void)configScrollView:(UIScrollView *)scrollView isCanTop:(BOOL)isCanTop;

 

- (void)scrollsToTop;

 

@end

 

復制代碼
復制代碼

#import "DDTopWindow.h"

 

static UIScrollView *scrollView_;

 

@interface DDTopWindow ()

 

@property (nonatomic, assign) BOOL isCan;

 

@end

 

@implementation DDTopWindow

 

SYNTHESIZE_SINGLETON_FOR_CLASS_ARC(DDTopWindow);

 

- (void)configScrollView:(UIScrollView *)scrollView isCanTop:(BOOL)isCanTop {

  scrollView_ = scrollView;

  self.isCan = isCanTop;

}

 

- (void)scrollsToTop {

  if (scrollView_.scrollEnabled) {

    [scrollView_ setContentOffset:CGPointMake(scrollView_.contentOffset.x, -scrollView_.contentInset.top) animated:YES];

  }

}

 

@end

 

復制代碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM