将UIPanGestureRecognizer锁定到某个方向

时间:2014-04-06 12:11:06

标签: ios objective-c ios7

我有UIPanGestureRecognizer,我试图让它只向一个方向滑动(向上)。 我还没有找到一个有效的解决方案。谢谢。 这是我目前的代码:

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

CGPoint t = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + t.x, recognizer.view.center.y + t.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];

}

1 个答案:

答案 0 :(得分:1)

- (void)panGesture:(UIPanGestureRecognizer *)recognizer {
    CGPoint t = [recognizer translationInView:self.view];

    if (t.y < 0) {
        t = CGPointMake(0, t.y);
    }
    else {
        t = CGPointMake(0, 0); // look at this
    }

    recognizer.view.center = CGPointMake(recognizer.view.center.x + t.x,
                                         recognizer.view.center.y + t.y);

    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}

为了能够上下拖动,请使用CGPointMake(0, t.y);而不是我的if