是否可以调整 UIScrollView 中的弹跳量?
例如,如果将 ScrollView 拖动到内容末尾 100pt 之外,则 ScrollView 会移动 50pt,我希望能够将相同拖动所经过的距离减少到 20pt。
我尝试了一些东西但没有成功。
调整 scrollViewDidScroll:
委托(delegate)方法中的 contentOffset
似乎是不可能的,因为当设置值并且内容偏移量接近零时,您会丢失原始滚动位置。
添加一个 transform
将 ScrollView 反向转换为内容偏移量的一部分似乎是个好主意。这会破坏所有的触摸跟踪并导致一系列问题。此外,在许多其他情况下,移动 ScrollView 会破坏 View 的布局。
您可以做的一件事是使用 UIView + UIPanGestureRecognizer 制作自己的 ScrollView 。
(有点过头了,这取决于你想要这个自定义效果有多糟糕。)
这里是 Apple 的 rubber band algorithm 的一个很好的解释.
它的核心是:
b = (1.0 – (1.0 / ((x * c / d) + 1.0))) * d
where:
x = distance from the edge
c = constant value, UIScrollView uses 0.55
d = dimension, either width or height
然后您可以使用 c 和 x 的值来获得您想要的效果。
一些代码片段可以帮助您入门:
- (IBAction)handlePanUIPanGestureRecognizer *)sender
{
static CGRect beginRect;
CGPoint translation = [sender translationInView:sender.view.superview];
CGPoint velocity = [sender velocityInView:sender.view.superview];
CGRect currentRect = CGRectOffset(beginRect, 0, translation.y);
if (sender.state == UIGestureRecognizerStateBegan) {
beginRect = sender.view.frame;
}
else if (sender.state == UIGestureRecognizerStateChanged) {
if (currentRect.origin.y > self.naturalHidePosition) {
CGFloat distanceFromEdge = currentRect.origin.y - self.naturalHidePosition;
CGFloat height = CGRectGetHeight(sender.view.frame);
CGFloat b = [self rubberBandDistance:distanceFromEdge dimension:height];
currentRect.origin.y = self.naturalHidePosition + b;
}
else if (currentRect.origin.y < self.naturalShowPosition) {
CGFloat distanceFromEdge = self.naturalShowPosition - currentRect.origin.y;
CGFloat height = CGRectGetHeight(sender.view.frame);
CGFloat b = [self rubberBandDistance:distanceFromEdge dimension:height];
currentRect.origin.y = self.naturalShowPosition - b;
}
sender.view.frame = currentRect;
}
else if (sender.state == UIGestureRecognizerStateEnded) {
if (velocity.y < 0) {
currentRect.origin.y = self.naturalShowPosition;
}
else if (velocity.y > 0) {
currentRect.origin.y = self.naturalHidePosition;
}
else if (currentRect.origin.y > (0.5 * (self.naturalShowPosition + self.naturalHidePosition))) {
currentRect.origin.y = self.naturalHidePosition;
}
else {
currentRect.origin.y = self.naturalShowPosition;
}
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
sender.view.frame = currentRect;
} completion:nil];
}
}
- (CGFloat)rubberBandDistanceCGFloat)distanceFromEdge dimensionCGFloat)d
{
CGFloat c = 0.55;
CGFloat b = (1.0 - (1.0 / ((distanceFromEdge * c / d) + 1.0))) * d;
return b;
}
关于ios - UIScrollView 调整弹性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22915664/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) | Powered by Discuz! X3.4 |