ios - UIScrollView 调整弹性
<p><p>是否可以调整 UIScrollView 中的弹跳量?</p>
<p>例如,如果将 ScrollView 拖动到内容末尾 100pt 之外,则 ScrollView 会移动 50pt,我希望能够将相同拖动所经过的距离减少到 20pt。</p>
<p>我尝试了一些东西但没有成功。</p>
<p>调整 <code>scrollViewDidScroll:</code> 委托(delegate)方法中的 <code>contentOffset</code> 似乎是不可能的,因为当设置值并且内容偏移量接近零时,您会丢失原始滚动位置。</p>
<p>添加一个 <code>transform</code> 将 ScrollView 反向转换为内容偏移量的一部分似乎是个好主意。这会破坏所有的触摸跟踪并导致一系列问题。此外,在许多其他情况下,移动 ScrollView 会破坏 View 的布局。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可以做的一件事是使用 UIView + UIPanGestureRecognizer 制作自己的 ScrollView 。</p>
<p>(有点过头了,这取决于你想要这个自定义效果有多糟糕。)</p>
<p>这里是 Apple 的 <a href="http://squareb.wordpress.com/2013/01/06/31/" rel="noreferrer noopener nofollow">rubber band algorithm</a> 的一个很好的解释.</p>
<p>它的核心是:</p>
<pre><code>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
</code></pre>
<p>然后您可以使用 c 和 x 的值来获得您想要的效果。</p>
<p>一些代码片段可以帮助您入门:</p>
<pre><code>- (IBAction)handlePan:(UIPanGestureRecognizer *)sender
{
static CGRect beginRect;
CGPoint translation = ;
CGPoint velocity = ;
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 = ;
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 = ;
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)rubberBandDistance:(CGFloat)distanceFromEdge dimension:(CGFloat)d
{
CGFloat c = 0.55;
CGFloat b = (1.0 - (1.0 / ((distanceFromEdge * c / d) + 1.0))) * d;
return b;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - UIScrollView 调整弹性,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/22915664/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/22915664/
</a>
</p>
页:
[1]