ios – 增加UIScrollView橡胶条带阻力

ios – 增加UIScrollView橡胶条带阻力,第1张

概述我有一个我想要在我的应用程序中实现的场景,当我开始强制它滚动超过其正常的内容界限时,我想使UIScrollView的行为更加“僵硬”. 我的意思是,当你在滚动视图的顶部或底部,如果你点击并继续拖动,你通常可以得到滚动视图,以保持超越其界限,但它逐渐建立抵抗,直到它在视野的中间通常约一半.当您抬起手指时,它会回到滚动区域的边界. 我想要实现的是,我想让这个“超越”的拖拽效果变得更加沉重,所以,而不是 我有一个我想要在我的应用程序中实现的场景,当我开始强制它滚动超过其正常的内容界限时,我想使UIScrollVIEw的行为更加“僵硬”.

我的意思是,当你在滚动视图的顶部或底部,如果你点击并继续拖动,你通常可以得到滚动视图,以保持超越其界限,但它逐渐建立抵抗,直到它在视野的中间通常约一半.当您抬起手指时,它会回到滚动区域的边界.

我想要实现的是,我想让这个“超越”的拖拽效果变得更加沉重,所以,而不是用户拖动滚动视图,并且它的“触底”中途通过滚动视图界限,它完全停止了大约20%左右的滚动边界.

我一直在试验覆盖scrollVIEwDIDScroll:delegate方法中的滚动视图的contentOffset,但这似乎没有起作用,因为在其中重新设置contentOffset似乎削弱了相同方法的进一步委托调用.

我的下一个想法是监视与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        }    }}

启用分页时,d回到休息点看起来似乎不能正常工作.橡胶带不是在页面边界处停止,而是以非页面偏移量超过它并停止.如果来自边缘的拉拽快速滑动,即使在抬起手指之后,在反转方向并返回到静止点之前,它也会沿着该方向继续移动.如果你只是暂停或离开,甚至轻轻地摆放到休息点,它似乎工作正常.为了解决这个问题,在下面的代码中,我尝试确定超调的可能性,并在返回时强制停止它,并试图跨越预期的页面边界.

class PageScrollVIEwDelegate : NSObject,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,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橡胶条带阻力全部内容,希望文章能够帮你解决ios – 增加UIScrollView橡胶条带阻力所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1110431.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存