有了这么一个需求,肯定是所有的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]));
}
简单记录下
网友评论