我知道如何识别滑动手势,但我不知道如何制作它会导致视图以动量向左或向右滑动。通过'动量'我的意思是它在手势结束后一直滑动一段时间并逐渐停止。我可以用平移手势移动视图,但视图很长,我需要比平移动作更快更快地移动它。视图是否必须位于UIScrollView
内?平移动作无需scrollView
。
答案 0 :(得分:1)
这是我用于用户在另一个视图中向上/向下拖动视图的一些代码,该视图在视图上使用手势识别器,并且在用户抬起手指后仍有动力继续。
只需进行必要的更改,使其适应视图的开始/结束位置,以及垂直或水平滚动等。
它不使用滚动视图。使用UIViewAnimationOptionCurveEaseOut可以实现动量,您可以尝试使用其他动画曲线来查看您喜欢的动画曲线,但是这一曲线会在您的帖子中描述“在手势结束后逐渐滑动一段时间并逐渐停止。” / p>
- (IBAction)handleVerticalSwipe:(id)sender
{
if ([sender state] == UIGestureRecognizerStateBegan)
{
self.pulldownViewStartPosition = self.pulldownView.frame.origin.y;
}
else
{
int maxYPosition = 527; // Calculate at run time
CGRect targetFrame = self.pulldownView.frame;
CGFloat newYPos = 0;
if ([sender state] == UIGestureRecognizerStateChanged)
{
// translationInView: returns a CGPoint which is not the position of the touch but the amount representing
// the total translation over time.
// The x and y values report the total translation over time. They are not delta values from the last time that
// the translation was reported. Therefore the translation value should be applied to the state of the view when
// the gesture is first recognized and should not be concatenated to the value each time the handler is called.
CGPoint translation = [sender translationInView: [self.view superview]];
newYPos = self.pulldownViewStartPosition + translation.y;
if (newYPos > maxYPosition)
{
newYPos = maxYPosition;
}
if (newYPos < self.pullDownDefaultPosition)
{
newYPos = self.pullDownDefaultPosition;
}
targetFrame.origin.y = newYPos;
self.pulldownView.frame = targetFrame;
}
else if ([sender state] == UIGestureRecognizerStateEnded) // User has lifted finger after swipe
{
// Gets the velocity of the gesture so it can be determined if the pulldown
// should extend or retract
CGFloat yVectorVelocity = [sender velocityInView: [self.view superview]].y;
if (yVectorVelocity < 0)
{
newYPos = self.pullDownDefaultPosition;
}
else
{
newYPos = maxYPosition;
}
targetFrame.origin.y = newYPos;
[UIView animateWithDuration:kPulldownDefaultAnimationDuration delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
self.pulldownView.frame = targetFrame;
}
completion:nil];
}
}
}
答案 1 :(得分:0)
是的,它听起来像UIScrollView正是您正在寻找的。我不确定你是否可以用“动量”实际禁用滚动,但你会发现它的行为与你需要的方式一样。
不是将UIView嵌套在UIScrollView中,您可能会发现UIScrollView可以执行您需要它执行的操作,而无需在其中嵌入其他UIView。我没有评论的声誉,但我想询问您遇到的具体问题,或者您想要“滚动”呈现的内容......?