我是iOS新手,我在我的项目中使用UIPanGestureRecognizer
。我在拖动视图时需要获取当前触摸点和上一个触摸点。我正在努力争取这两点。
如果我使用touchesBegan
方法而不是使用UIPanGestureRecognizer
,我可以通过以下代码获得这两点:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint touchPoint = [[touches anyObject] locationInView:self];
CGPoint previous=[[touches anyObject]previousLocationInView:self];
}
我需要在UIPanGestureRecognizer
事件火灾方法中得到这两点。我怎样才能做到这一点?请指导我。
UITouch中有一个功能可以在视图中进行上一次触摸
您应该如下实例化您的平移手势识别器:
UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
然后你应该在你的视图中添加panRecognizer:
[aView addGestureRecognizer:panRecognizer];
当用户与视图交互时,将调用- (void)handlePan:(UIPanGestureRecognizer *)recognizer
方法。在handlePan:你可以得到这样的点:
CGPoint point = [recognizer locationInView:aView];
您还可以获取panRecognizer的状态:
if (recognizer.state == UIGestureRecognizerStateBegan) {
//do something
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
//do something else
}
如果您不想存储任何内容,您也可以这样做:
let location = panRecognizer.location(in: self)
let translation = panRecognizer.translation(in: self)
let previousLocation = CGPoint(x: location.x - translation.x, y: location.y - translation.y)