动作当我在iPhone中持有UIButton 2秒时触发 [英] Action Trigger when I hold UIButton for 2 second in iPhone
本文介绍了动作当我在iPhone中持有UIButton 2秒时触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序中有一个 UIButton
,当我触摸 UIButton
时会触发一个动作。
I have a UIButton
in my application and an action which is triggered when I TouchDown UIButton
.
是否可以在iPhone上的 UIButton
上检测到触摸并保持?我希望当用户按住按钮2秒钟或更长时间时触发我的操作。
Is it possible to detect a Touch-and-Hold on the UIButton
on the iPhone? I want my action to trigger when the user holds the button for 2 seconds or more.
任何想法?
推荐答案
UILongPressGestureRecognizer
是您所需要的。例如,
UILongPressGestureRecognizer
is what you need. For example,
UILongPressGestureRecognizer *longPress_gr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(doAction:)];
[longPress_gr setMinimumPressDuration:2]; // triggers the action after 2 seconds of press
[yourButton addGestureRecognizer:longPress_gr];
让你的行动只被触发一次(即,当2秒持续时间结束时),确保你的 doAction:
方法看起来像这样,
To let your action get triggered only once(ie., when the 2 seconds duration is over), make sure you have your doAction:
method looks something like this,
- (void)doAction:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
// Your code here
}
}
这篇关于动作当我在iPhone中持有UIButton 2秒时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文