UIPanGestureRecognizer:在滑动结束时检测触摸位置

时间:2015-01-10 17:53:15

标签: ios uitouch uipangesturerecognizer uiswipegesturerecognizer

我在touchesEnded方法中调用了一个名为addProjectile的方法。 addProjectile接收touchesEnded方法接收的触摸的NSSet。为简单起见,我只在相关代码中发布了我的问题。所以,只是要明确:

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self addProjectile:touches]; }
-(void) addProjectile:(NSSet *)touches {//do stuff }

我想在名为swipeRight的UIPanGestureRecognizer方法的末尾调用addProjectile并发送正确的NSSet触摸。

-(void)swipedRight:(UIPanGestureRecognizer *)recognizer {
    CGPoint panned=[recognizer translationInView:self.view];
    if(panned.x>50){//do stuff }
    else {
    NSSet *touches; <-- this is what I need to get
    [self addProjectile:touches];

所以我的问题是如何在swipedRight结束时获得正确的NSSet触摸(这是用户拿起他/她的手指):正确执行addProjectile方法。

1 个答案:

答案 0 :(得分:7)

看看UIGestureRecognizerState。这就是你所需要的。

示例代码(假设您在用户完成平移时只需要触摸点,即抬起手指):

-(void)swipedRight:(UIPanGestureRecognizer *)panGesture {

    if ([panGesture state] == UIGestureRecognizerStateEnded) {

        //User touch has ended

        CGPoint touchUpPoint = [panGesture translationInView:self.view]; // or `locationInView:`

        NSLog(@"__TOUCH_END_POINT__ = %@", NSStringFromCGPoint(touchUpPoint));
    }
}