UIButton长按手指不动

UIButton long press with finger stationary(UIButton长按手指不动)

本文介绍了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长按手指不动

基础教程推荐