如何向多个按钮添加手势识别器?

发布于 2024-12-09 08:49:30 字数 1013 浏览 0 评论 0原文

你好,我正在尝试向“UIButton”添加手势识别器。当我这样做时:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.LeftBottomSpaceBtn addGestureRecognizer:singleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
[singleTap release];

它工作正常,但是当我尝试将此手势添加到多个按钮时,它不起作用:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.LeftBottomSpaceBtn addGestureRecognizer:singleTap];
[self.LeftUpSpaceBtn addGestureRecognizer:singleTap];
[self.RightBUpSpaceBtn addGestureRecognizer:singleTap];
[self.LeftReturnBtn addGestureRecognizer:singleTap];
[self.RightReturnBtn addGestureRecognizer:singleTap];
[self.DeleteBtn addGestureRecognizer:singleTap];
[self.CapsBtn addGestureRecognizer:singleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
[singleTap release];

那么如何以与添加“longPress”相同的方式将相同的手势添加到多个按钮“双击”?

Hi, I am trying to add gesture recognizers to 'UIButton'. When I do it like this:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.LeftBottomSpaceBtn addGestureRecognizer:singleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
[singleTap release];

It works properly, but when I tried to add this gesture to multiple buttons it did not work:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.LeftBottomSpaceBtn addGestureRecognizer:singleTap];
[self.LeftUpSpaceBtn addGestureRecognizer:singleTap];
[self.RightBUpSpaceBtn addGestureRecognizer:singleTap];
[self.LeftReturnBtn addGestureRecognizer:singleTap];
[self.RightReturnBtn addGestureRecognizer:singleTap];
[self.DeleteBtn addGestureRecognizer:singleTap];
[self.CapsBtn addGestureRecognizer:singleTap];
[singleTap requireGestureRecognizerToFail:doubleTap];
[singleTap release];

So how can I add the same gesture to multiple buttons in the same way I have added the 'longPress' and 'doubleTap'?

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

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

发布评论

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

评论(2

怪我入戏太深 2024-12-16 08:49:30

我建议如下:

NSMutableSet *buttons = [[NSMutableSet alloc] init];

[buttons addObject: self.LeftBottomSpaceBtn];
[buttons addObject: self.LeftUpSpaceBtn];
[buttons addObject: self.RightBUpSpaceBtn];
[buttons addObject: self.LeftReturnBtn];
[buttons addObject: self.RightReturnBtn];
[buttons addObject: self.DeleteBtn];
[buttons addObject: self.CapsBtn];

for(UIButton *button in buttons)
{
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [button addGestureRecognizer:singleTap];
    [singleTap requireGestureRecognizerToFail:doubleTap];
    [singleTap release];
}

如果将集合保存为变量,则还可以对所有按钮执行其他操作,例如释放所有按钮并更改所有背景颜色,而无需单独调用它们。

您可能还需要为每个按钮制作单独的 doubletaprecognizer。

I'd suggest the following:

NSMutableSet *buttons = [[NSMutableSet alloc] init];

[buttons addObject: self.LeftBottomSpaceBtn];
[buttons addObject: self.LeftUpSpaceBtn];
[buttons addObject: self.RightBUpSpaceBtn];
[buttons addObject: self.LeftReturnBtn];
[buttons addObject: self.RightReturnBtn];
[buttons addObject: self.DeleteBtn];
[buttons addObject: self.CapsBtn];

for(UIButton *button in buttons)
{
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [button addGestureRecognizer:singleTap];
    [singleTap requireGestureRecognizerToFail:doubleTap];
    [singleTap release];
}

If you save the set as a variable, you can do other stuff for all buttons as well, such as releasing them all and changing all their backgroundColors, without calling them all individually.

You will probably need to make seperate doubletaprecognizers for each button as well.

活雷疯 2024-12-16 08:49:30

您可以将一个手势识别器单独添加到一个视图。如果将其添加到多个视图,则最后添加的视图将添加识别器。

创建手势识别器的不同实例并将它们添加到各个视图中。

You can add one gesture recognizer to one view alone. If you add it to more than one views, the last added view will be added with the recognizer.

Create different instances of gesture recognizers and add them to individual views.

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