我想要一个仅水平移动 UIView 的平移手势。
到目前为止,在平移手势开始时,只允许水平移动,但是一旦开始平移手势,UIView 就会水平和垂直移动。
- 如何阻止 UIView 始终垂直移动?
- 有什么办法可以让 UIView 的顶部始终位于屏幕的顶部?
即它永远不会从其设定位置垂直移动
这是我当前的代码:
- (void)panePanned:(UIPanGestureRecognizer *)gestureRecognizer
{
switch (gestureRecognizer.state) {
case UIGestureRecognizerStateBegan: {
self.paneStartLocation = [gestureRecognizer locationInView:self.mainView];
self.paneVelocity = 0.0;
break;
}
case UIGestureRecognizerStateChanged: {
CGPoint panLocationInPaneView = [gestureRecognizer locationInView:self.mainView];
CGFloat velocity = -(self.paneStartLocation.x - panLocationInPaneView.x);
CGRect newFrame = self.mainView.frame;
newFrame.origin.x += (panLocationInPaneView.x - self.paneStartLocation.x);
if (newFrame.origin.x < 0.0) newFrame.origin.x = 0.0;
self.mainView.frame = newFrame;
if (velocity != 0) {
self.paneVelocity = velocity;
}
break;
}
case UIGestureRecognizerStateEnded: {
[self animate];
break;
}
default:
break;
}
}
谢谢!