UIBut上的UITapGestureRecognizer [英] UITapGestureRecognizer on a UIButton

查看:124
本文介绍了UIBut上的UITapGestureRecognizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIButton,它有一个IBAction和一个UITapGestureRecognizer来检测双击。

I have a UIButton which has an IBAction as well as a UITapGestureRecognizer to detect double taps.

目前看来IBAction正在阻止识别器。有没有办法阻止这个或UITapGestureRecognizer甚至可以在按钮上工作?如果是这样,最好只添加识别器并删除IBActions吗?

At the moment it looks like the IBAction is blocking the recognizer. Is there a way to stop this or do UITapGestureRecognizer even work on buttons? If so wouldn't it be better to just add the recognizers and remove the IBActions?

EDIT

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[A1 addGestureRecognizer:doubleTap];
[A2 addGestureRecognizer:doubleTap];
[B1 addGestureRecognizer:doubleTap];


推荐答案

看起来你正试图将一个手势识别器附加到多个按钮。手势识别器一次只能附加到一个视图。因此,在您的情况下,您将识别器附加到(按钮B1)的最后一个按钮可能响应双击,但A1和A2不响应。

Looks like you are trying to attach one gesture recognizer to multiple buttons. A gesture recognizer can only be attached to one view at a time. So in your case, the last button you are attaching the recognizer to (button B1) probably responds to the double tap but A1 and A2 don't.

创建一个单独的每个按钮的识别器。

但是所有三个识别器都可以调用相同的操作方法( handleDoubleTap:)。

Create a separate recognizer for each button.
But all three recognizers can call the same action method (handleDoubleTap:).

然而,当你尝试单击一个按钮时,会有一点延迟,因为它等待看它是否是双击的开始。有一些方法可以减少延迟,但如果您能忍受延迟并且解决方法会带来其他问题,则可能不值得。

However, when you try to do a single tap on a button, there will be a slight delay as it waits to see if it's the beginning of a double tap. There are ways to reduce the delay but may not be worth it if you can live with the delay and the workarounds bring up other issues.

编辑:

在评论中,您说想要检测它们是否同时被按下。为此,您不需要手势识别器。您可以使用提供的标准控制事件。


In your comment, you say you "want to detect if they are pressed at the same time". To do this, you don't need gesture recognizers. You can just use the standard control events provided.

接下来,在IB中,对于 每个 按钮,请勾选带有按钮的Touch Down事件:。或者,以编程方式执行:

Next, in IB, for each button, hook up the "Touch Down" event with buttonPressed:. Or, to do it programmatically:

[button1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button2 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button3 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];

接下来,在IB中, 每个 按钮,使用按钮释放:,将内部触摸 触摸外部事件连接起来。或者,要做到这一点编程:

Next, in IB, for each button, hook up the "Touch Up Inside" and "Touch Up Outside" events with buttonReleased:. Or, to do it programmatically:

[button1 addTarget:self action:@selector(buttonReleased:) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
[button2 addTarget:self action:@selector(buttonReleased:) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];
[button3 addTarget:self action:@selector(buttonReleased:) forControlEvents:UIControlEventTouchUpInside|UIControlEventTouchUpOutside];

接下来,添加ivars以跟踪按下的按钮数量:

Next, add ivars to keep track of how many or which buttons are pressed:

@property (nonatomic) int numberOfButtonsBeingTouched;
@property (strong, nonatomic) NSMutableSet *buttonsBeingTouched; //alloc + init in viewDidLoad or similar

如果您只关心按下了多少个按钮,不需要 NSMutableSet

If you just care how many buttons are pressed, you don't need the NSMutableSet.

最后,添加buttonPressed和buttonReleased方法:

Finally, add the buttonPressed and buttonReleased methods:

- (IBAction)buttonPressed:(UIButton *)button {
    self.numberOfButtonsBeingTouched++;
    [self.buttonsBeingTouched addObject:button];
    //your logic here (if (self.numberOfButtonsBeingTouched == 3) ...)
}

- (IBAction)buttonReleased:(UIButton *)button {
    self.numberOfButtonsBeingTouched--;
    [self.buttonsBeingTouched removeObject:button];
    //your logic (if any needed) here
}

这篇关于UIBut上的UITapGestureRecognizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆