[英]On a UILongPressGestureRecognizer how do I detect which object generated the event?
I have a view with several UIButtons. 我有几个UIButton的视图。 I have successfully implemented using UILongPressGestureRecognizer with the following as the selector;
我使用UILongPressGestureRecognizer成功实现了以下作为选择器;
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateEnded ) {
NSLog(@"Long Press");
}
}
What I need to know within this method is which UIButton received the longpress since I need to do something different, depending on which button received the longpress. 在这种方法中我需要知道的是UIButton接收了长按,因为我需要做一些不同的事情,这取决于哪个按钮接收了长按。
Hopefully the answer is not some issue of mapping the coordinates of where the longpress occured to the bounds of the buttons - would rather not go there. 希望答案不是将长按发生的位置的坐标映射到按钮边界的问题 - 而不是去那里。
Any suggestions? 有什么建议?
Thanks! 谢谢!
这在gesture.view
可用。
Are you adding the long tap gesture controller to the UIView that has the UIButtons as subviews? 您是否将长按手势控制器添加到将UIButtons作为子视图的UIView? If so, something along the lines of @Magic Bullet Dave's approach is probably the way to go.
如果是这样的话,那么@Magic Bullet Dave的方法可能就是这样。
An alternative is to subclass UIButton and add to each UIButton a longTapGestureRecogniser. 另一种方法是子类化UIButton并向每个UIButton添加longTapGestureRecogniser。 You can then get your button to do what you like.
然后,您可以按下按钮来执行您喜欢的操作。 For example, it could send a message identifying itself to a view controller.
例如,它可以向视图控制器发送标识自身的消息。 The following snippet illustrates methods for the subclass.
以下代码段说明了子类的方法。
- (void) setupLongPressForTarget: (id) target;
{
[self setTarget: target]; // property used to hold target (add @property and @synthesise as appropriate)
UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:button action:@selector(longPress:)];
[self addGestureRecognizer:longPress];
[longPress release];
}
- (void) longPress: (UIGestureRecognizer*) recogniser;
{
if (![recogniser isEnabled]) return; // code to prevent multiple long press messages
[recogniser setEnabled:NO];
[recogniser performSelector:@selector(setEnabled:) withObject: [NSNumber numberWithBool:YES] afterDelay:0.2];
NSLog(@"long press detected on button");
if ([[self target] respondsToSelector:@selector(longPressOnButton:)])
{
[[self target] longPressOnButton: self];
}
}
In your view controller you might have code something like this: 在视图控制器中,您可能拥有以下代码:
- (void) viewDidLoad;
{
// set up buttons (if not already done in Interface Builder)
[buttonA setupLongPressForTarget: self];
[buttonB setupLongPressForTarget: self];
// finish any other set up
}
- (void) longPressOnButton: (id) sender;
{
if (sender = [self buttonA])
{
// handle button A long press
}
if (sender = [self buttonB])
{
// handle button B long press
}
// etc.
}
If your view contains multiple subViews (like lots of buttons) you can determine what was tapped: 如果您的视图包含多个子视图(如许多按钮),您可以确定被点击的内容:
// Get the position of the point tapped in the window co-ordinate system
CGPoint tapPoint = [gesture locationInView:nil];
UIView *viewAtBottomOfHeirachy = [self.window hitTest:tapPoint withEvent:nil];
if ([viewAtBottomOfHeirachy isKindOfClass:[UIButton class]])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.