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长按手指不动
基础教程推荐
- 我的 UIImageView 的任务 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
