iOS - UILongPressGestureRecognizer [英] iOS - UILongPressGestureRecognizer
本文介绍了iOS - UILongPressGestureRecognizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我希望 UILongPressGestureRecognizer 每秒发送一次连续的消息,直到按钮被释放.不幸的是,没有像连续"这样的状态,所以我需要使用开始"和结束"来控制我的消息.这是我到目前为止的代码,我在终端上获得了两个日志,但 while 循环没有停止?
In my app I want the UILongPressGestureRecognizer will send continuous messages every second until the button is released. Unfortunately there is no state like "continuous" so I need to use "began" and "ended" to control my messages. Here is my code I have so far, I get both logs on the terminal but the while loop is not stopping?
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
BOOL loop = NO;
if (gesture.state == UIGestureRecognizerStateBegan) {
NSLog(@"Long press detected.");
loop = YES;
while (loop){
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
// here I want to do my stuff every second
}
} else if (gesture.state == UIGestureRecognizerStateEnded) {
NSLog(@"Long press Ended");
loop = NO;
}
}
有人可以帮忙吗?
推荐答案
使用计时器(或可能隐藏计时器的 performSelector:withDelay:
)是一个很好的建议,但是,大致:
Its a good suggestion to use a timer (or a performSelector:withDelay:
which probably conceals a timer) but, roughly:
@property(strong,nonatomic) NSTimer *timer;
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if (gesture.state == UIGestureRecognizerStateBegan) {
NSLog(@"started");
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(stillPressing:) userInfo:nil repeats:YES];
} else if (gesture.state == UIGestureRecognizerStateEnded) {
[self.timer invalidate];
self.timer = nil;
NSLog(@"ended");
}
}
- (void)stillPressing:(NSTimer *)timer {
NSLog(@"still pressing");
}
这篇关于iOS - UILongPressGestureRecognizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文