objective-c – 自定义UIGestureRecognizer:选择器不接收UIGest
我正在实现自定义UIGestureRecognizer.为简单起见,假设它识别包含> 1触摸的手势.
这是Gesture.m: #import "Gesture.h" #import <UIKit/UIGestureRecognizerSubclass.h> #define SHOW printf("%s %d %d %dn",__FUNCTION__,self.state,touches.count,self.numberOfTouches) @implementation Gesture - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { SHOW; if (self.numberOfTouches==1) return; self.state = UIGestureRecognizerStateBegan; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { SHOW; if (self.numberOfTouches==1) return; self.state = UIGestureRecognizerStateChanged; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { SHOW; if (self.numberOfTouches==1) return; self.state = UIGestureRecognizerStateEnded; } @end 这是一个选择器: - (IBAction)handleGesture:(Gesture *)recognizer { printf("%s %dn",recognizer.state); } 这是一个输出: -[Gesture touchesBegan:withEvent:] 0 1 1 // 1st touch began -[Gesture touchesMoved:withEvent:] 0 1 1 -[Gesture touchesMoved:withEvent:] 0 1 1 -[Gesture touchesMoved:withEvent:] 0 1 1 -[Gesture touchesBegan:withEvent:] 0 1 2 // 2nd touch began -[Gesture touchesMoved:withEvent:] 1 1 2 // Gesture.state==UIGestureRecognizerStateBegan but selector was not called -[ViewController handleGesture:] 2 // UIGestureRecognizerStateChanged received. -[Gesture touchesMoved:withEvent:] 2 2 2 -[ViewController handleGesture:] 2 -[Gesture touchesMoved:withEvent:] 2 2 2 -[ViewController handleGesture:] 2 -[Gesture touchesMoved:withEvent:] 2 2 2 -[ViewController handleGesture:] 3 // UIGestureRecognizerStateEnded received. 为什么选择器不接收UIGestureRecognizerStateBegan? 解决方法
这对我来说并不是特别明显,但规则似乎是:
>通过touchesBegan发送给你的所有触摸:…随后被视为属于你,除非你将状态设置为UIGestureRecognizerStateFailed,UIGestureRecognizerStateEnded或UIGestureRecognizerStateCancelled; 所以你不应该为自己设置UIGestureRecognizerStateChanged – 只需跟踪触摸并正确发布开始,结束,失败和取消. 在你的情况下,我认为你只需要删除touchesMoved中的状态集:…. (除此之外:以上是适用于iOS 5和6;在4下,行为稍微有些微妙.要在所有三个版本下工作,请使用if(self.state == UIGestureRecognizerStateBegan)self.state = UIGestureRecognizerStateChanged;当你知道时您的属性已更改) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |