我正在实现自定义 UIGestureRecognizer。为简单起见,假设它识别由 >1 次触摸组成的手势。
这是手势.m:
#import "Gesture.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
#define SHOW printf("%s %d %d %d\n", __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 %d\n", __FUNCTION__, 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?