uitableview - 将 scrollViewWillEndDragging 中的 targetContentOffset 更新为错误方向的值不会产生动画

标签 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
    }
}

我可以潜在地计算出这个“错误”何时会出现,并可能使用另一种“快速滚动”方式。关于如何执行此操作或使用 targetContentOffset.pointee.y 解决它的任何建议像平常一样?

最佳答案

我找到了一个不错的解决方案(或解决方法)。如果有人有更好的解决方案,请告诉我。

我刚刚检测到不想要的“非动画快速滚动”何时出现,然后没有将新值设置为 targetContentOffset.pointee.y我将其设置为当前偏移值(停止它)并使用 scrollViews setContentOffset(_:animated:) 设置所需的偏移目标值反而

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)
    }
}

关于uitableview - 将 scrollViewWillEndDragging 中的 targetContentOffset 更新为错误方向的值不会产生动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44563230/

相关文章:

ios - 如何在 ViewWillAppear 中向自定义 UITableViewCell 添加边框

swift - 如何垂直对齐 Table View Cell 的所有内容?

objective-c - Obj-C,消息表达式中的接收者是未初始化的值,分析器警告?

iphone - 子类化 MKAnnotationView 并重写 setDragState

ios - 将 UIView 从一个 UIScrollView 拖到另一个 UIScrollView

ios - UIScrollView 水平滚动,自动布局分页

ios - Xcode:Table View Cell 不会自动调整其高度

ios - 从 URL 字符串中提取的 UITableViewController 中的图像加载缓慢

iphone - 如何暂停 UIImageView 动画

ios - UIScrollView 的内容在导航栏下方有偏移