Pan Gesture已经结束了位置

时间:2014-04-21 18:59:27

标签: objective-c uigesturerecognizer

我尝试这样做,当它结束平移手势时,检查结束位置是否在某个区域内,在我的情况下是一个正方形。我有以下代码,它更少工作,但由于某种原因,即使平底锅在广场结束,它说不。

-(void)panHandler:(UIPanGestureRecognizer*)recognizer{
CGPoint translation = [recognizer translationInView:self.view];
switch (recognizer.state){
    case UIGestureRecognizerStateBegan:
        _stickImage.center = CGPointMake(_stickImage.center.x + translation.x, _stickImage.center.y + translation.y);
        break;

    case UIGestureRecognizerStateChanged:
        _stickImage.center = CGPointMake(_stickImage.center.x + translation.x, _stickImage.center.y + translation.y);
        [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
        break;

    case UIGestureRecognizerStateEnded:
        NSLog(@"Ended");
        if(translation.x > _rectView.frame.origin.x && translation.x < (_rectView.frame.origin.x + _rectView.frame.size.width)){
            if(translation.y > _rectView.frame.origin.y && translation.y < (_rectView.frame.origin.y + _rectView.frame.size.height)){
                NSLog(@"In the grid!");
            }
        }
        else{
            NSLog(@"out of the grid");
        }

        break;

    default:
        break;
}

}

我可以像我一样使用翻译x和y位置吗?或者我如何找到平底锅位置是否在广场?非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:4)

translationInView:未给出触摸位置,它会告诉您自上次拨打setTranslation:inView:

以来触摸的移动距离

您想要的方法是locationInView:

CGPoint location = [recognizer locationInView:recognizer.view];

if(CGRectContainsPoint(_rectView.frame, location)){
    NSLog(@"Inside!");
}