UIButton Long Press Event(UIButton 长按事件)
问题描述
我想模拟一个长按按钮,我该怎么做?我认为需要一个计时器.我看到 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 长按事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIButton 长按事件


- GPS状态的广播接收器? 2022-01-01
- UITextView 内容插图 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- URL编码Swift iOS 2022-01-01