如何向多个按钮添加手势识别器?
你好,我正在尝试向“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议如下:
如果将集合保存为变量,则还可以对所有按钮执行其他操作,例如释放所有按钮并更改所有背景颜色,而无需单独调用它们。
您可能还需要为每个按钮制作单独的 doubletaprecognizer。
I'd suggest the following:
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.
您可以将一个手势识别器单独添加到一个视图。如果将其添加到多个视图,则最后添加的视图将添加识别器。
创建手势识别器的不同实例并将它们添加到各个视图中。
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.