最近使用UIPanGestureRecognizer手势时遇到一个问题,就是想获取起始的触摸点,但UIPanGestureRecognizer手势需要滑动一点距离时,才会触发,那样获取的起始点不太准确。然后就想到了
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
这个方法,但这个方法还是有个缺点,就是开始触摸时,需要稍微停顿一下才会触发,如果你轻扫的话,就不会调用,显然效果不太理想。
那我们可以继承UIPanGestureRecognizer,然后重写UIPanGestureRecognizer的
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
方法,然后调用父类的touchesBegan方法就可以了,代码片段如下:
#import "ImmediatePanGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
@implementation ImmediatePanGestureRecognizer
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
self.state = UIGestureRecognizerStateBegan;
}
@end
你也可以从上面的重写方法中,直接得到point
UITouch *touch=[touches anyObject];
CGPoint startPoint =[touch locationInView:touch.view];