xcode 按钮上的手势识别器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6135483/
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
Gesture recognizer on button
提问by Joetjah
I'd like to implement a gesture recognizer (swipe action) for a button. The problem is, the buttons are create programmatically and are or aren't existent based on a few conditions. So, I don't know if there are buttons, or how many.
我想为按钮实现手势识别器(滑动操作)。问题是,按钮是以编程方式创建的,并且根据一些条件是否存在。所以,我不知道是否有按钮,或者有多少。
I know I need something like:
我知道我需要这样的东西:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (touch.view == aButtonView) {
//get the button's tag
}
}
Of course, the if-statement should return Yes when any button view is pressed...
当然,当按下任何按钮视图时,if 语句应该返回 Yes...
Anyone has any idea on what the word aButtonView
should be? Or if it's even possible? Thanks in advance.
任何人都知道这个词aButtonView
应该是什么?或者,如果它甚至可能?提前致谢。
回答by Deepak Danduprolu
You should think about using UISwipeGestureRecognizer
instances. Attach the gesture recognizer to the button objects -
您应该考虑使用UISwipeGestureRecognizer
实例。将手势识别器附加到按钮对象 -
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSwipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionUp;
[button addGestureRecognizer:swipe];
[swipe release];
and in handleSwipe:
并在 handleSwipe:
- (void) handleSwipe:(UISwipeGestureRecognizer *)swipe {
NSInteger tag = swipe.view.tag;
}
它应该是
if ( [gestureRecognizer.view isKindOfClass:[UIButton class]] ) {
if ( [gestureRecognizer.view isKindOfClass:[UIButton class]] ) {