将UIView移动限制为仅垂直或水平轴

时间:2015-02-13 15:34:51

标签: ios objective-c swipe

我正在使用PanGestureRecognizer,在UIGestureRecognizerStateChanged中,我让用户手指移动屏幕上的视图。我正在使用它作为Tinder,如滑动手势,我现在想要限制视图移动到水平轴或垂直轴,无论用户开始滑动的方向。我一直在上下搜索,但在这里找不到合适的东西。

根据用户开始滑动视图的方向,是否有任何聪明的方法来限制轴移动?

非常感谢!

更新:以下是移动视图的当前代码:

- (void)dragged:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGFloat xDistance = [gestureRecognizer translationInView:self].x;
    CGFloat yDistance = [gestureRecognizer translationInView:self].y;
//    xDistance = 0;

    [parentView dragged:yDistance];

    switch (gestureRecognizer.state) {
        case UIGestureRecognizerStateBegan:{
            self.originalPoint = self.center;
            break;
        };
        case UIGestureRecognizerStateChanged:{
            CGFloat rotationAngel = 0;
            CGFloat scale = 1;//MAX(scaleStrength, 0.93);
            CGAffineTransform transform = CGAffineTransformMakeRotation(rotationAngel);
            CGAffineTransform scaleTransform = CGAffineTransformScale(transform, scale, scale);
            self.transform = scaleTransform;
            self.center = CGPointMake(self.originalPoint.x + xDistance, self.originalPoint.y + yDistance);
            ...
            break;
        };
        case UIGestureRecognizerStateEnded: {
            float moveDistAction = 60;
            if (yDistance > moveDistAction) {
                // view swiped down
                ...
            } else if (yDistance < -moveDistAction) {//100 150
                // view swiped up 
                ...
            } else {
                // dragging cancelled
                ...
            }
            break;
        };
        case UIGestureRecognizerStatePossible:break;
        case UIGestureRecognizerStateCancelled:break;
        case UIGestureRecognizerStateFailed:break;
    }
}

2 个答案:

答案 0 :(得分:5)

gestureRecognizerShouldBegin:中你可以知道这个方向:

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    CGPoint translation = [gestureRecognizer translationInView:self.view];
    self.isVerticalPan = fabs(translation.y) > fabs(translation.x); // BOOL property

    return YES;
}

然后在UIGestureRecognizerStateChanged您可以根据isVerticalPan属性执行此类操作:

CGPoint translation = [gesture translationInView:self.view];
CGPoint displacement = (self.isVerticalPan) ? CGPointMake(0, translation.y) : CGPointMake(translation.x, 0);

self.viewToMove.transform = CGAffineTransformMakeTranslation(displacement.x, displacement.y);

答案 1 :(得分:3)

您可以通过实施gestureRecognizerShouldBegin:委托方法来实现此目的。在下面的实现中,我正在检查大约20度的水平滑动。但是,您可以检查水平或垂直滑动。 (我会使用一些小角度,因为用户很难直线移动。)

- (BOOL) gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    // Ensure that the user is only panning mostly horizontally
    // This allows the view to be placed in a scrollview that scrolls vertically
    // Up/Down drags in the body of the view will be passed to the scroll view
    if (gestureRecognizer == self.panGesture) {

        CGPoint velocity = [self.panGesture velocityInView:self.panGesture.view];

        double radian = atan(velocity.y/velocity.x);
        double degree = radian * 180 / M_PI;

        double thresholdAngle = 20.0;
        if (fabs(degree) > thresholdAngle) {
            return NO;
        }
    }
    return YES;
}

免责声明:我很确定我在这里的某个地方找到了这个解决方案,但我无法找到它。所以,向这个原始求解者道歉...