我有一个UIButton
在我的应用程序中以及当我 TouchDown 时触发的操作UIButton
.
是否可以检测到触摸并按住UIButton
在iPhone上?我希望当用户按住按钮 2 秒或更长时间时触发我的操作。
有任何想法吗?
UILongPressGestureRecognizer
就是你所需要的。例如,
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:
方法看起来像这样,
- (void)doAction:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
// Your code here
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)