UIButton用手指固定长按 [英] UIButton long press with finger stationary

查看:166
本文介绍了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");
}






更新
在编码中你可以像这样写


Updated In coding you can write like this

[btn addTarget:self action:@selector(touchUp:) forControlEvents:UIControlEventTouchUpInside];
[btn addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];

和xib

这篇关于UIButton用手指固定长按的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆