将 UIView 移动限制为仅垂直或水平轴
我正在使用 PanGestureRecognizer,在 UIGestureRecognizerStateChanged 中,我让用户手指移动屏幕上的视图.我将它用于 Tinder 之类的滑动手势,现在我想将视图的移动限制为水平轴或垂直轴,无论用户开始滑动的方向.我一直在上下搜索,但没有找到任何适合这里的东西.
I'm using a PanGestureRecognizer and in the UIGestureRecognizerStateChanged I let a view on the screen be moved by the users finger. I'm using that for a Tinder like swipe away gesture and I now want to limit the movement of the view to either the horizontal axis or the vertical axis, whatever direction the user started to swipe. I've been searching up and down but didn't find anything suitable here.
有没有什么聪明的方法可以根据用户开始滑动视图的方向来限制轴移动?
Is there any clever way to limit the axis movement, based on which direction the user started to swipe the view?
非常感谢!
更新:这是移动视图的当前代码:
- (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;
}
}
在gestureRecognizerShouldBegin:
中你可以知道那个方向:
In gestureRecognizerShouldBegin:
you can know that direction:
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
CGPoint translation = [gestureRecognizer translationInView:self.view];
self.isVerticalPan = fabs(translation.y) > fabs(translation.x); // BOOL property
return YES;
}
然后在 UIGestureRecognizerStateChanged
上,您可以根据 isVerticalPan
属性执行以下操作:
And then on the UIGestureRecognizerStateChanged
you could do something like this, based on isVerticalPan
property:
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);