gpt4 book ai didi

ios - 增加 UIScrollView 橡皮筋阻力

转载 作者:技术小花猫 更新时间:2023-10-29 10:10:11 25 4
gpt4 key购买 nike

我尝试在我的应用程序中实现一个场景,我希望在您开始强制 UIScrollView 滚动超过其正常内容时表现得更“僵硬”边界。

我的意思是,当你在 ScrollView 的顶部或底部时,如果你点击并继续拖动,你通常可以让 ScrollView 保持滚动超出其边界,但它逐渐建立阻力,直到它通常在 View 边界的中间大约一半处停止。当您抬起手指时,它会弹回滚动区域的边界。

我想要实现的是,我想让“越界”的拖动效果更加沉重,而不是用户拖动 ScrollView 并在中间“触底”通过 ScrollView 边界的方式,它完全停止在超过其滚动边界的 20% 左右。

我一直在尝试在 scrollViewDidScroll: 委托(delegate)方法中覆盖 ScrollView 的 contentOffset,但这似乎不起作用,因为在似乎搞砸了同一方法的进一步委托(delegate)调用。

我的下一个想法是监视与 ScrollView 关联的 UIPanGestureRecognizer,并尝试根据由此产生的事件确定正确的 UIScrollView contentOffset。话虽如此,我认为这可能会开始变得棘手,所以我想在尝试可能会造成困惑的事情之前,我会在这里询问我没有考虑过的任何其他解决方案。

谢谢!

最佳答案

我正在使用 Swift 编写以下代码。这是用于水平滚动,可以很容易地适应垂直滚动。解决方案根据是否启用分页而有所不同。两者都在下面给出。

class ScrollViewDelegate : NSObject, UIScrollViewDelegate
{
let maxOffset: CGFloat // offset of the rightmost content (scrollview contentSize.width - frame.width)
var prevOffset: CGFloat = 0 // previous offset (after adjusting the value)

var totalDistance: CGFloat = 0 // total distance it would have moved (had we not restricted)
let reductionFactor: CGFloat = 0.2 // percent of total distance it will be allowed to move (under restriction)
let scaleFactor: CGFloat = UIScreen.mainScreen().scale // pixels per point, for smooth translation in respective devices

init(maxOffset: CGFloat)
{
self.maxOffset = maxOffset // scrollView.contentSize.width - scrollView.frame.size.width
}

func scrollViewDidScroll(scrollView: UIScrollView)
{
let flipped = scrollView.contentOffset.x >= maxOffset // dealing with left edge or right edge rubber band
let currentOffset = flipped ? maxOffset - scrollView.contentOffset.x : scrollView.contentOffset.x // for right edge, flip the values as if screen is folded in half towards the left

if(currentOffset <= 0) // if dragging/moving beyond the edge
{
if(currentOffset <= prevOffset) // if dragging/moving beyond previous offset
{
totalDistance += currentOffset - prevOffset // add the "proposed delta" move to total distance
prevOffset = round(scaleFactor * totalDistance * reductionFactor) / scaleFactor // set the prevOffset to fraction of total distance
scrollView.contentOffset.x = flipped ? maxOffset - prevOffset : prevOffset // set the target offset, after negating any flipping
}
else // if dragging/moving is reversed, though still beyond the edge
{
totalDistance = currentOffset / reductionFactor // set totalDistance from offset (reverse of prevOffset calculation above)
prevOffset = currentOffset // set prevOffset
}
}
else // if dragging/moving inside the edge
{
totalDistance = 0 // reset the values
prevOffset = 0
}
}
}

启用分页后,弹回静止点似乎没有按预期工作。橡皮筋不是停在页面边界,而是越过它并停在非页面偏移处。如果边缘的拉力快速轻弹,即使在您抬起手指后它继续沿该方向移动,然后反转方向并返回到静止点,就会发生这种情况。如果您只是暂停并离开,甚至将其弹回到休息点,它似乎工作正常。为了解决这个问题,在下面的代码中,我尝试确定超调的可能性,并在它返回并尝试越过预期的页面边界时强行停止它。

class PageScrollViewDelegate : NSObject, UIScrollViewDelegate
{
let maxOffset: CGFloat // offset of the rightmost content (scrollview contentSize.width - frame.width)
var prevOffset: CGFloat = 0 // previous offset (after adjusting the value)

var totalDistance: CGFloat = 0 // total distance it would have moved (had we not restricted)
let reductionFactor: CGFloat = 0.2 // percent of total distance it will be allowed to move (under restriction)
let scaleFactor: CGFloat = UIScreen.mainScreen().scale // pixels per point, for smooth translation in respective devices

var draggingOver: Bool = false // finger dragging is over or not
var overshoot: Bool = false // is there a chance for page to overshoot page boundary while falling back

init(maxOffset: CGFloat)
{
self.maxOffset = maxOffset // scrollView.contentSize.width - scrollView.frame.size.width
}

func scrollViewWillBeginDragging(scrollView: UIScrollView)
{
draggingOver = false // reset the flags
overshoot = false
}

func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool)
{
draggingOver = true // finger dragging is over
}

func scrollViewDidScroll(scrollView: UIScrollView)
{
let flipped = scrollView.contentOffset.x >= 0.5 * maxOffset // dealing with left edge or right edge rubber band
let currentOffset = flipped ? maxOffset - scrollView.contentOffset.x : scrollView.contentOffset.x // for right edge, flip the values as if screen is folded in half towards the left

if(currentOffset <= 0) // if dragging/moving beyond the edge
{
if(currentOffset <= prevOffset) // if dragging/moving beyond previous offset
{
overshoot = draggingOver // is content moving farther away even after dragging is over (caused by fast flick, which can cause overshooting page boundary while falling back)

totalDistance += currentOffset - prevOffset // add the "proposed delta" move to total distance
prevOffset = round(scaleFactor * totalDistance * reductionFactor) / scaleFactor // set the prevOffset to fraction of total distance
scrollView.contentOffset.x = flipped ? maxOffset - prevOffset : prevOffset // set the target offset, after negating any flipping
}
else // if dragging/moving is reversed, though still beyond the edge
{
totalDistance = currentOffset / reductionFactor // set totalDistance from offset (reverse of prevOffset calculation above)
prevOffset = currentOffset // set prevOffset
}
}
else // if dragging/moving inside the edge
{
if(overshoot) // if this movement is a result of overshooting
{
scrollView.setContentOffset(CGPointMake(flipped ? maxOffset : 0, scrollView.contentOffset.y), animated: false) // bring it to resting point and stop further scrolling (this is a patch to control overshooting)
}

totalDistance = 0 // reset the values
prevOffset = 0
}
}
}

关于ios - 增加 UIScrollView 橡皮筋阻力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20437657/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com