iOS 一段时间没有操作后,执行某操作

有了这么一个需求,肯定是所有的touch事件,但是通常我们知道的是touchBegan等触摸事件,可是这些方法在点击按钮等方法时就没有反应了,查了很久后找到了一个方法

新建一个继承与UIApplication的类


@interface **OC类名字** : UIApplication

@property (nonatomic,strong)NSTimer *Timer;

-(void)resetTimer;

@end

然后实现文件.m


-(void)sendEvent:(UIEvent *)event{

[super sendEvent:event];

if (!self.Timer) {

[self resetTimer];

}

NSSet *allTouches = [event allTouches];

if ([allTouches count]>0) {

UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;

if (phase == UITouchPhaseBegan) {

[self resetTimer];

}

}

}


-(void)resetTime{

if (self.shutDownTimer) {

[self.Timer invalidate];

}

self.Timer = [NSTimer scheduledTimerWithTimeInterval:**设定不操作后时间** target:self selector:@selector(notifyToAction) userInfo:nil repeats:NO];

}


-(void)notifyToAction{

[[NSNotificationCenter defaultCenter]postNotificationName:@"ActionD" object:nil];

}

然后在需要的地方接受ActionD的通知去执行操作。最后还要做的就是,去main.m

引入你所新建类的头文件


@autoreleasepool {

return UIApplicationMain(argc, argv, NSStringFromClass([**新建类** class]), NSStringFromClass([AppDelegate class]));

}

简单记录下

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

推荐阅读更多精彩内容