如何为x或y轴创建二进制平移手势识别器

时间:2014-01-28 09:44:42

标签: ios objective-c uigesturerecognizer uipangesturerecognizer

我想创建一个平移手势识别器并将其附加到视图中。

手势应仅考虑其中一个轴X或Y上的移动。

到目前为止我所拥有的是非常粗暴的

        UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
        [self addGestureRecognizer:recognizer];

-(void)handlePan:(UIPanGestureRecognizer *)recognizer
{
    UIGestureRecognizerState state = recognizer.state;
    CGPoint translation = [recognizer translationInView:self];
    NSLog(@"state = %d : panned to %@",state,NSStringFromCGPoint(translation));


    if ((recognizer.state == UIGestureRecognizerStateChanged) ||
        (recognizer.state == UIGestureRecognizerStateEnded))
    {
        CGFloat translationX = translation.x;
        if (ABS(translation.y) > 20 && ABS(translation.y) > ABS(translation.x))
        {
            // this is not a valid x pan because the vector has to much "y" movement in it
        }
        // need to do similar for x axis
    }
}

有没有更好的方法来实现这一目标?

1 个答案:

答案 0 :(得分:0)

如果您不介意在ScrollView中嵌入视图,那么您只需将directionalLockEnabled设置为YES,即可完成此操作。您还可以取出滚动指示器,使其看起来与UIPanGestureRecognizer相同。