触摸事件不共享问题
在iOS中,当某个触摸事件被UIGestureRecongnizer对象识别出来,就会“吃掉”所有相关的触摸事件,导致其他UIGestureRecongnizer对象无法识别出这个事件
解决方法:代理
1.UIView的子类
self.panGestureRecognizer= [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)]; self.panGestureRecognizer.delegate = self; self.panGestureRecognizer.cancelsTouchesInView=NO; [self addGestureRecognizer:self.panGestureRecognizer];
其中 cancelsTouchesInView 默认设置为YES,设置为YES的时候,该对象在识别出某个手势后,会“吃掉”跟手势有关的UITouch对象,这样对象所依附的UIView对象将无法接受到相关 Responder消息。例如,touchBegin:withEven
2.实现
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 方法
当gesture recognizer 识别出手势,同时其他gesture recognizer也识别出手势,geesture recongnizer 会向协议代理发送消息,如果该方法返回YES,recognizer 会跟其他gesture recognizer 共享手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if (gestureRecognizer==self.panGestureRecognizer) { return YES; } return NO; }
状态
- UIGestureRecognizerStateBegan: 当手指移动时,gestureRecognizer就会进入此状态,持续向target发送消息(action)
- UIGestureRecognizerStateChanged:当手指移动时,gestureRecognizer就会进入此状态,持续向target发送消息(action)
- UIGestureRecognizerStateEnded: 当手指离开屏幕的时候,gestureRecognizer对象会切换至结束状态,向target发送最后一次消息