How to reset NSTimer for touch , touch move in ios

2019-08-08 15:00发布

I am working on app in which i have to hide control button after 3 second so i write code using NSTimer and Touch began and it work but problem is that when i touch again with on any another button than my timer is not reset and even if i move my touch example like drag.

If i drag or move touch it should reset the timer but it won't.

I found that this implementation work if i continuously touch on other area (but it is not working on Control button) IF i continuously touch control button still it goes hidden after 3 second.How to solve this problem. i want event to fire on button click also.

EDITED I solve my problem by own.. I have added this code on button click sector and it works..

Thank you for all support

if(screenTimer)
{
    [screenTimer invalidate];
    screenTimer = nil;
    screenTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(turnOffScreen) userInfo:nil repeats:NO];
}

Here is my code

// Touch began for touch event.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

if(screenTimer)
{
    [screenTimer invalidate];
    screenTimer = nil;
}

mode1View.hidden=NO;

 screenTimer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self       selector:@selector(turnOffScreen) userInfo:nil repeats:NO];
}

- (void)turnOffScreen{
NSLog(@"TURN OFF SCREEN");
if(screenTimer!=nil)
{
    mode1View.hidden=YES;
}
}

Any help is appreciated. Thank you

1条回答
冷血范
2楼-- · 2019-08-08 15:45

have you tried

[self performSelector:@selector(turnOffScreen:) withObject:nil afterDelay:3.0];

The NSObject method performSelector:withObject:afterDelay: it invoke a methods on the object with an objec after a certain delay.

查看更多
登录 后发表回答