继承UIAplication拦截所有点击事件

暂离需求

当用户在一个子界面一段时间(3分钟)没有进行动作时,则自动返回到初始界面

分析


  • 已知点击事件在iOS里分为两种。
  1. target action模式
  2. UITapGestureRecognizer手势
  • 已知以上两种模式在点击时候必然调用UIApplicationtouchesBegan:withEvent:

  • 已知使用倒计时->取消倒计时 原理,可以用来解决延时请求以及暂离计数问题

  • 已知可以发送通知到各个界面,用来处理延时之后进行的操作

结论
在touch开始时,取消延时请求,在touch结束后,开始延时请求,发送通知


实现

继承UIApplication,重写sendEvent:

extern NSString *const kTSAFKNotification;

interface TSAplication : UIApplication

@(原创整理)end

.m

@implementation TSAplication

NSString *const kTSAFKNotification = @"TSAFKNotification";

- (void)sendEvent:(UIEvent *)event
{
    NSSet *touches = [event allTouches];
    
    for (UITouch *touch in touches) {
        
        switch ([touch phase]) {
                
            case UITouchPhaseBegan:
                NSLog(@"UITouchPhaseBegan:%s",__PRETTY_FUNCTION__);
                [[self class] cancelPreviousPerformRequestsWithTarget:self];
                break;
                
            case UITouchPhaseMoved:
                NSLog(@"%s",__PRETTY_FUNCTION__);
                
                break;
                
            case UITouchPhaseEnded:
            case UITouchPhaseCancelled:
                NSLog(@"%s %tu",__PRETTY_FUNCTION__,[touch phase]);
                [self performSelector:@selector(delayEvent) withObject:nil afterDelay:5];
                break;
                
            default:
                break;
        }
    }
    
    [super sendEvent:event];
}

- (void)delayEvent
{
    [[NSNotificationCenter defaultCenter] postNotificationName:kTSAFKNotification object:nil];
}

Main函数里引入

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, @"TSAplication", NSStringFromClass([AppDelegate class]));
    }
}

测试

用2个Controller来模拟AFK功能,ViewController使用UIButton以及UILabelGesture事件来控制跳入SubController,在收到AFK消息之后,SubController消失

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(90, 100, 200, 40)];
    [btn addTarget:self action:@selector(_openSubController) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"切换到下个界面(button)" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UILabel *gestureLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 180, CGRectGetWidth(self.view.bounds), 100)];
    gestureLabel.textAlignment = NSTextAlignmentCenter;
    gestureLabel.text = @"切换到下个界面(gesture)";
    gestureLabel.textColor = [UIColor blueColor];
    gestureLabel.userInteractionEnabled = YES;
    [self.view addSubview:gestureLabel];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_tap:)];
    [gestureLabel addGestureRecognizer:tap];
}

测试target action

- (void)_openSubController
{
    SubController *sc = [[SubController alloc] init];
    [self presentViewController:sc  animated:YES completion:^{
        
    }];
}

测试UITapGestureRecognizer

- (void)_tap:(UITapGestureRecognizer*)aTap
{
    [self _openSubController];
}

Demo

预览

  • ViewController
继承UIAplication拦截所有点击事件_第1张图片
截屏2016_12_6_上午10_15.png
  • SubController
继承UIAplication拦截所有点击事件_第2张图片
截屏2016_12_6_上午10_16.png

代码

TSAFKDemo

你可能感兴趣的:(继承UIAplication拦截所有点击事件)