我的应用程序中有一个 View 占据了屏幕的下半部分。我想让用户向下拖动 View ,这样它只占屏幕底部的 1/4。然后用户可以将其向上拖动以再次占据屏幕的 1/2。所以就像 iOS7 中的底部向上滑动 View (即手电筒、计算器、音量等...)。我需要以完全相同的方式运行,但不要让它完全离开屏幕。
所以我只需要保持平移垂直。我还希望它在覆盖一半屏幕时停止,然后在覆盖屏幕的 1/4 时停止另一个方向。我还希望它在拖过某个点时卡入到位,这样只有两个位置是屏幕上的 1/2 和 1/4。所以我不希望用户能够在屏幕上拖动它的 1/3。
这是我目前所拥有的,但它允许我将 View 抛出或抛出超过最大停止点。
- (IBAction)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *piece = [gestureRecognizer view];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
CGPoint vel = [gestureRecognizer velocityInView:piece];
if (vel.y > 0)
{
// user dragged towards the right
//counter++;
if(piece.frame.origin.y >= (self.view.frame.size.height - piece.frame.size.height) ) {
// don't update
} else {
[piece setCenter:CGPointMake([piece center].x, [piece center].y + translation.y)];
}
} else {
// user dragged towards the left
//counter--;
NSLog(@"");
if(piece.frame.origin.y <= 205) {
// don't update
} else {
[piece setCenter:CGPointMake([piece center].x, [piece center].y + translation.y)];
}
}
[gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}
最佳答案
查看此示例项目中的 UIResponder 文档和触摸手势: https://developer.apple.com/library/ios/samplecode/Touches/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007435
关于ios - 仅垂直平移具有弹性效果的 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22212847/