继承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
截屏2016_12_6_上午10_15.png
  • SubController
截屏2016_12_6_上午10_16.png

代码

TSAFKDemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,214评论 30 472
  • -- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实...
    翘楚iOS9阅读 3,008评论 0 13
  • 好奇触摸事件是如何从屏幕转移到APP内的?困惑于Cell怎么突然不能点击了?纠结于如何实现这个奇葩响应需求?亦或是...
    Lotheve阅读 58,111评论 51 603
  • 在iOS开发中经常会涉及到触摸事件。本想自己总结一下,但是遇到了这篇文章,感觉总结的已经很到位,特此转载。作者:L...
    WQ_UESTC阅读 6,133评论 4 26
  • 在开发过程中,大家或多或少的都会碰到令人头疼的手势冲突问题,正好前两天碰到一个类似的bug,于是借着这个机会了解了...
    闫仕伟阅读 5,431评论 2 23