UIButton 长按事件

UIButton Long Press Event(UIButton 长按事件)
本文介绍了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 长按事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)