xcode 手指静止长按 UIButton

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9389923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:24:00  来源:igfitidea点击:

UIButton long press with finger stationary

iphonexcodeuibuttonpressure

提问by AleMal

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.

在我的项目中,我需要使用 UIButton(或其他组件)来处理使用长按的事件。让我解释一下,我应该记住我按住按钮一个计时器来计算秒数并释放到压力停止,我尝试管理 UILongPressGestureRecognizer 但事实并非如此,因为我记得按下按钮时的事件但是仅当我移动手指时,但我希望计时器消失并在按住按钮(手指静止)的所有时间进行计数,并在松开手指时停止计数。

Does anyone know how to help me? Thanks

有谁知道如何帮助我?谢谢

回答by Inder Kumar Rathore

Use these two methods for buttons events. touchDownis called when you press the button and touchUpwill be called when you lift your finger from the button. Calculate the time difference between these two methods. Also you can start timer in touchDownand stop/restart it in touchUp.

将这两种方法用于按钮事件。touchDown当您按下按钮touchUp时调用,当您从按钮上松开手指时将调用。计算这两种方法之间的时间差。您也可以touchDowntouchUp.

//connect this action with Touch up inside
- (IBAction)touchUp:(id)sender {
    NSLog(@"up");
}

//connect this to tocuh down
- (IBAction)touchDown:(id)sender{
    NSLog(@"down");
}


UpdatedIn coding you can write like this

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

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

and in xib enter image description here

并在xib 在此处输入图片说明

回答by Krunal

The same I have done...As u said about UILongPressGestureRecognizer, I can't understand it..but u can write ur code inside-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { }. I have done same by using this method and got successful result..:). You even don't need to add timer,instead u can use...

我也做过同样的事情......正如你所说的 UILongPressGestureRecognizer,我无法理解......但你可以在里面写你的代码-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { }。我使用这种方法做了同样的事情并得到了成功的结果..:)。您甚至不需要添加计时器,而是可以使用...

 UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
                                      initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 1.0;
lpgr.delegate = self;

I think this works perfectly..

我认为这很有效..