美文网首页
iOS状态栏点击监听

iOS状态栏点击监听

作者: iOS_Developer | 来源:发表于2016-10-18 19:17 被阅读626次

在appDelegate中添加点击监听,发送通知即可
objective-C:


- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    
    CGPoint touchLocation = [[[event allTouches] anyObject] locationInView:self.window];
    CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
    NSLog(@"location.x = %f,location.y = %f",touchLocation.x,touchLocation.y);
    if (CGRectContainsPoint(statusBarFrame, touchLocation))
    {
       //发送通知
      //发通知,监听通知不用我教你了吧,哈哈哈
    }
}

swift:


override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        let location = (touches as NSSet).anyObject()?.locationInView(self.window)
        
        let statusBarFrame = UIApplication.sharedApplication().statusBarFrame
        
        if CGRectContainsPoint(statusBarFrame, location!) {
            
            //发送通知
            print("发送通知")
            NSNotificationCenter.defaultCenter().postNotificationName("StatusBarClickNotification",
                                                                      object: self, userInfo: nil)
            print("通知完毕")

        }
        
    }

相关文章

网友评论

      本文标题:iOS状态栏点击监听

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