简体   繁体   English

如何重置NSTimer进行触摸,在iOS中触摸移动

[英]How to reset NSTimer for touch , touch move in ios

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. 我正在开发其中必须在3秒后隐藏控制按钮的应用程序,因此我使用NSTimer编写了代码,Touch开始了,并且可以正常工作,但是问题是,当我再次触摸任何其他按钮而不是计时器时,即使重置,即使我像拖动一样移动我的触摸示例。

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. 我发现如果我连续触摸其他区域(但是它在控制按钮上不起作用),如果我连续触摸控制按钮仍然在3秒后隐藏起来,则该实现有效。如何解决此问题。 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 谢谢

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. NSObject方法performSelector:withObject:afterDelay:经过一定的延迟后,它将使用performSelector:withObject:afterDelay:调用对象上的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM