sol*_*oid 6 objective-c nsdate nstimer nsthread ios
我正在开发我的第一个iOS应用程序,并且遇到了第一个问题我无法找到一个好的答案.
问题:我有一个自定义UIGestureRecognizer
并且所有连线都正确连接,我可以@selector
在识别后为每次触摸运行代码.这对大多数事情来说都很好,但对其他人来说这是一个太多的输入.
我的目标:制作一个以指定间隔触发的计时器来运行逻辑,并且能够在取消触摸时取消此计时.
我在这里问的原因:解决方案有很多可能性,但没有一个能够成为最好的解决方案.到目前为止似乎
performSelector
(以及对此的一些变化)NSThread
NSTimer
NSDate
从所有的研究来看,某种形式的线程似乎是可行的路线,但我不知道哪种方式最适合这种情况.
实现的示例:NSPoint
每0.10秒采用一次,并且采用前一点和当前点之间的距离.[取每个点之间的距离产生非常混乱的结果].
相关代码:
- (void)viewDidLoad {
CUIVerticalSwipeHold *vSwipe =
[[CUIVerticalSwipeHold alloc]
initWithTarget:self
action:@selector(touchHoldMove:)];
[self.view addGestureRecognizer:vSwipe];
[vSwipe requireGestureRecognizerToFail:doubleTap];
}
...
- (IBAction)touchHoldMove:(UIGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
}
if (sender.state == UIGestureRecognizerStateBegan) {
}
//other stuff to do goes here
}
Run Code Online (Sandbox Code Playgroud)
小智 11
使用NSTimer
设置如下:
theTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(yourMethodThatYouWantRunEachTimeTheTimerFires) userInfo:nil repeats:YES];
Run Code Online (Sandbox Code Playgroud)
然后当你想取消它时,做这样的事情:
if ([theTimer isValid])
{
[theTimer invalidate];
}
Run Code Online (Sandbox Code Playgroud)
请注意,在上面的示例中,您需要声明NSTimer的"theTimer"实例,它可用于两种方法.在上面的例子中,"0.5"表示计时器每秒会发射两次.根据需要调整.