问题描述
- 多个gestureRecognizer问题
- 在parentClass(继承自UIView)中:
[self addGestureRecognizer:_tapGesture]; // _tapGesture is UITapGestureRecognizer with delegate on parentClass
在someClass中:
[_myImageView addGestureRecognizer:_imageViewGestureRecognizer]; // _imageViewGestureRecognizer is UITapGestureRecognizer with delegate on someClass
问题:我每次点击ImageView时,两个手势都会被触发。我只要_imageViewGestureRecognizer 触发。
试过:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch { UIView *gestureView = recognizer.view; CGPoint point = [touch locationInView:gestureView]; UIView *touchedView = [gestureView hitTest:point withEvent:nil]; if ([touchedView isEqual:_imageViewGestureRecognizer]) { return NO; } return YES;}
解决方案
拿到附加手势的view后,判断view的类型,即可相应做出处理
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch { UIView *gestureView = recognizer.view; if ([gestureView isKindOfClass:[UIImageView class]]) { ///...to do }}
时间: 2024-11-03 08:25:42