增加UIScrollView橡皮筋阻力

TiM*_*TiM 15 iphone objective-c uiscrollview ipad ios

我有一个场景,我正试图在我的应用程序中实现,UIScrollView一旦你开始强迫它滚动超过其正常的内容范围,我想让一个表现更"僵硬".

我的意思是,当你在滚动视图的顶部或底部时,如果你点击并继续拖动,你通常可以获得滚动视图以保持滚动超出其范围,但它逐渐建立阻力,直到它通常停在视图中间的一半左右.抬起手指时,它会快速回到滚动区域的边界.

我想要实现的是,我想让"越界"的拖拽效果更加沉重,所以不是用户拖动滚动视图而是"触底"中途通过滚动视图边界,它完全停止在其滚动边界的20%左右.

我一直在尝试重写滚动视图contentOffset里面scrollViewDidScroll:的委托方法,但似乎并没有工作,因为重新设置contentOffset在那里似乎搞砸了同样的方法,进一步委托调用.

我的下一个想法是监视UIPanGestureRecognizer与scrollview相关联的内容,并尝试UIScrollView根据其中的事件确定正确的contentOffset.话虽这么说,我认为可能会开始走上黑客的一面,所以我想我会在这里问任何其他我没想过的解决方案,然后再尝试一些可能会很混乱的东西.

谢谢!

pro*_*ist 5

我在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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)