简体   繁体   English

在UILongPressGestureRecognizer上,如何检测生成事件的对象?

[英]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.

相关问题 如何在 Swift 中将 UILongPressGestureRecognizer 与 UICollectionViewCell 一起使用? - How do I use UILongPressGestureRecognizer with a UICollectionViewCell in Swift? 如何用动画结束UILongPressGestureRecognizer? - How do I end UILongPressGestureRecognizer with an animation? 如何启用一次触摸即可处理UILongPressGestureRecognizer和UIPanGestureRecognizer? - How do I enable a single touch to handle both UILongPressGestureRecognizer and UIPanGestureRecognizer? 一次由UILongPressGestureRecognizer触发拖动时,如何防止UIView“跳动”? - How do I keep my UIView from “jumping” when dragging it once triggered by UILongPressGestureRecognizer? 如何在ARKitExample中检测我在sceneView中触摸过哪一个对象? - How to detect which one object i touched in sceneView at ARKitExample? UIPopoverController 在“点击关闭”时关闭 - 我如何检测此事件? - UIPopoverController dismiss on 'tap off' - how do I detect this event? 要知道触发了哪个UILongPressGestureRecognizer - To know which UILongPressGestureRecognizer is triggered 如何检测UIImageview的动画方向? - How do I detect which direction a UIImageview is animating? UILongPressGestureRecognizer 不做任何事情 - UILongPressGestureRecognizer does not do anything 如何检测对象是否已在Sprite Kit中刷过 - How do I detect if object has been swiped in sprite kit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM