将scrollViewWillEndDragging中的targetContentOffset更新为错误方向的值不会为

时间:2017-06-15 09:01:33

标签: uitableview cocoa-touch uiscrollview uiscrollviewdelegate

我在targetContentOffset中为scrollViewWillEndDragging(_:withVelocity:targetContentOffset:)设置了新值,以便在UITableView中创建自定义分页解决方案。当设置targetContentOffset的坐标时,它按预期工作,该坐标与速度指向的滚动方向相同。

然而,当在速度的相反方向上“向后”捕捉时,它立即“快速回弹”而没有动画。这看起来很糟糕。关于如何解决这个问题的任何想法。

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    // a lot of caluculations to detemine where to "snap scroll" to
    if shouldSnapScrollUpFromSpeed || shouldSnapScrollUpFromDistance {
        targetContentOffset.pointee.y = -scrollView.frame.height
    } else if shouldSnapScrollDownFromSpeed || shouldSnapScrollDownFromDistance {
        targetContentOffset.pointee.y = -detailViewHeaderHeight
    }
}

我可以潜在地计算出这个“bug”何时出现,并且可能使用另一种“快速滚动”方式。有关如何执行此操作或使用targetContentOffset.pointee.y正常解决此问题的任何建议?

1 个答案:

答案 0 :(得分:0)

I found an okey solution (or workaround). Let me know if anyone have a better solution.

I just detected when the undesired "non-animated snap scroll" would appear and then instead of setting a new value to targetContentOffset.pointee.y I set it to the current offset value (stopped it) and set the desired offset target value with scrollViews setContentOffset(_:animated:) instead

if willSnapScrollBackWithoutAnimation {
    targetContentOffset.pointee.y = -scrollView.frame.height+yOffset //Stop the scrolling
    shouldSetTargetYOffsetDirectly = false
}

if let newTargetYOffset = newTargetYOffset {
    if shouldSetTargetYOffsetDirectly {
        targetContentOffset.pointee.y = newTargetYOffset
    } else {
        var newContentOffset = scrollView.contentOffset
        newContentOffset.y = newTargetYOffset
        scrollView.setContentOffset(newContentOffset, animated: true)
    }
}