【发布时间】:2011-06-16 11:06:57
【问题描述】:
我的应用程序中有一个UIButton 和一个在我触碰UIButton 时触发的操作。
是否可以在 iPhone 上检测到 UIButton 上的触摸并按住?我希望当用户按住按钮 2 秒或更长时间时触发我的操作。
有什么想法吗?
【问题讨论】:
标签: ios4 ios-simulator iphone
我的应用程序中有一个UIButton 和一个在我触碰UIButton 时触发的操作。
是否可以在 iPhone 上检测到 UIButton 上的触摸并按住?我希望当用户按住按钮 2 秒或更长时间时触发我的操作。
有什么想法吗?
【问题讨论】:
标签: ios4 ios-simulator iphone
另一方面,您可以使用this NBTouchAndHoldButton。这正是你想要的,而且很容易实现:
TouchAndHoldButton * pageDownButton = [TouchAndHoldButton buttonWithType:UIButtonTypeCustom];
[pageDownButton addTarget:self action:@selector(pageDownAction:) forTouchAndHoldControlEventWithTimeInterval:0.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
}
}
【讨论】: