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