UIButton long press with finger stationary(UIButton长按手指不动)
问题描述
在我的项目中,我需要使用 UIButton(或其他组件)通过长按来处理事件.让我解释一下,我应该记住我按住按钮一个计时器来计算秒数并释放压力停止,我尝试使用 UILongPressGestureRecognizer 的管理但不是这种情况,因为我记得按下按钮时的事件但是仅当我移动手指时,但我希望计时器消失并计数所有按住按钮的时间(手指静止)并在松开手指时停止计数.
In my project I have the need to use a UIButton (or another component) to handle events using long press. Let me explain, I should make that mind I hold down the button a timer to count the seconds and release to pressure stop, I tried with the management of UILongPressGestureRecognizer but is not the case because I recall the event when the button is held down but only if I move my finger, but I wish the timer went away and counted all the time in which the button is held down (with your finger stationary) and stopped counting when the finger is released.
有人知道如何帮助我吗?谢谢
Does anyone know how to help me? Thanks
推荐答案
对按钮事件使用这两种方法.touchDown
在你按下按钮时被调用,touchUp
在你手指离开按钮时被调用.计算这两种方法之间的时间差.您也可以在 touchDown
中启动计时器并在 touchUp
中停止/重新启动它.
Use these two methods for buttons events. touchDown
is called when you press the button and touchUp
will be called when you lift your finger from the button. Calculate the time difference between these two methods. Also you can start timer in touchDown
and stop/restart it in touchUp
.
//connect this action with Touch up inside
- (IBAction)touchUp:(id)sender {
NSLog(@"up");
}
//connect this to tocuh down
- (IBAction)touchDown:(id)sender{
NSLog(@"down");
}
<小时>
更新在编码中你可以这样写
[btn addTarget:self action:@selector(touchUp:) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
在 xib
这篇关于UIButton长按手指不动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UIButton长按手指不动
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01