xcode 如何将手势识别器添加到多个按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7751998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add gesture recognizers to multiple buttons?
提问by Nik's
Hi, I am trying to add gesture recognizers to 'UIButton'. When I do it like this:
嗨,我正在尝试向“UIButton”添加手势识别器。当我这样做时:
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'?
那么如何以与添加“longPress”和“doubleTap”相同的方式向多个按钮添加相同的手势?
回答by Aberrant
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.
您可能还需要为每个按钮制作单独的 doubletaprecognizers。
回答by EmptyStack
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.
创建手势识别器的不同实例并将它们添加到各个视图中。