在 UILongPressGestureRecognizer 上,如何检测哪个对象生成了事件?

发布于 2024-12-04 01:54:55 字数 400 浏览 4 评论 0原文

我有几个 UIButton 的视图。我已成功使用 UILongPressGestureRecognizer 实现,并使用以下内容作为选择器;

- (void)longPress:(UILongPressGestureRecognizer*)gesture {
    if ( gesture.state == UIGestureRecognizerStateEnded ) {
        NSLog(@"Long Press");
    }
}

在这个方法中我需要知道的是哪个 UIButton 收到了长按,因为我需要做一些不同的事情,具体取决于哪个按钮收到了长按。

希望答案不是将长按发生的坐标映射到按钮边界的问题 - 宁愿不去那里。

有什么建议吗?

谢谢!

I have a view with several UIButtons. I have successfully implemented using UILongPressGestureRecognizer with the following as the selector;

- (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.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神经大条 2024-12-11 01:54:55

这可以在 gesture.view 中找到。

This is available in gesture.view.

浅听莫相离 2024-12-11 01:54:55

您是否将长按手势控制器添加到将 UIButtons 作为子视图的 UIView 中?如果是这样,@Magic Bullet Dave 的方法可能是正确的选择。

另一种方法是子类化 UIButton 并向每个 UIButton 添加一个 longTapGestureRecogniser。然后,您可以使用按钮来执行您喜欢的操作。例如,它可以将标识自己的消息发送到视图控制器。以下代码片段说明了子类的方法。

- (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];
    }
}

在你的视图控制器中你可能有这样的代码:

- (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.

 }

Are you adding the long tap gesture controller to the UIView that has the UIButtons as subviews? If so, something along the lines of @Magic Bullet Dave's approach is probably the way to go.

An alternative is to subclass UIButton and add to each UIButton a 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.

 }
梨涡少年 2024-12-11 01:54:55

如果您的视图包含多个子视图(如许多按钮),您可以确定点击的内容:

// 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]])

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]])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文