问题描述
我想模拟一个长按按钮,我该怎么做?我认为需要一个计时器.我看到 UILongPressGestureRecognizer
但我该如何使用这种类型?
I want to emulate a long a press button, how can I do this? I think a timer is needed.
I see UILongPressGestureRecognizer
but how can I utilize this type?
推荐答案
您可以从创建 UILongPressGestureRecognizer
实例并将其附加到按钮开始.
You can start off by creating and attaching the UILongPressGestureRecognizer
instance to the button.
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self.button addGestureRecognizer:longPress];
[longPress release];
然后实现处理手势的方法
And then implement the method that handles the gesture
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateEnded ) {
NSLog(@"Long Press");
}
}
现在这将是基本方法.您还可以设置压力机的最短持续时间以及可以容忍的错误量.另请注意,如果您在识别手势后调用该方法几次,因此如果您想在结束时执行某些操作,则必须检查其状态并进行处理.
Now this would be the basic approach. You can also set the minimum duration of the press and how much error is tolerable. And also note that the method is called few times if you after recognizing the gesture so if you want to do something at the end of it, you will have to check its state and handle it.
这篇关于UIButton 长按事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!