ios - 当动画为假时,如何检测 UIScrollView 已完成滚动?

标签 ios swift uiscrollview uicollectionview

我有一个名为 timeline 的 collectionView,它在此处的倒数第二行以编程方式滚动:

internal func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        if !onceOnly {
            let indexToScrollTo = IndexPath(row: self.posts.count - 1, section: 0)
            collectionView.scrollToItem(at: indexToScrollTo, at: .left, animated: false)
            let firstPost = posts.first?.timeStamp
            let firstOfFirstMonth = firstPost?.startOfMonth()
            let diff = posts.last?.timeStamp.months(from: firstOfFirstMonth!)
            //self.currentPostMonth = diff
            let monthCellIndexPath = IndexPath(row: diff!, section: 0)
            timeline.scrollToItem(at: monthCellIndexPath, at: .centeredHorizontally, animated: false)
            onceOnly = true
        }
    }

稍后..我正在尝试检测这已经完成滚动

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
    if scrollView == timeline {
        print("Did finish")
    }
}

func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
    if scrollView == timeline {
        print("Did finish")
    }
}

滚动完成后,两个打印语句都不会触发。我认为部分是因为 animation = false。当我将其设置为 true 时,它​​打印 Did finish correctly - 我认为特别是 scrollViewDidEndScrollingAnimation 打印,即使 scrollViewDidEndDecelerating 仍然不执行任何操作,因为它以编程方式滚动。

我怎样才能检测到这个卷轴已经完成了?

最佳答案

我认为是因为您将 collectionView 与 scrollView 进行了比较

if scrollView == timeline

这是 false ,顺便说一句,你可以使用

func scrollViewDidScroll(_ scrollView: UIScrollView)

如果没有动画那么在之后的下一行

timeline.scrollToItem(at: monthCellIndexPath, at: .centeredHorizontally, animated: false)

运行你想要的,因为它会在滚动成功结束的地方连续发生,无论是否调用,都不需要在 scrollView 的委托(delegate)方法中设置它

关于ios - 当动画为假时,如何检测 UIScrollView 已完成滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52174319/

相关文章:

ios - 如何在 Swift 2.3 的 ScrollView 中仅禁用缩小功能?

ios - as3 组件,类似于 iOS 的 UIScrollView

ios - 如果选中复选框,如何使用 UserDefaults 保留电子邮件和密码文本字段信息 Swift iOS

iOS 虚拟内存 : does iOS automatically page out static const arrays when memory is scarce?

ios - 午睡框架请求取消

iPhone - UIScrollView 和 UIDatePicker 滚动冲突 : the one interfer with the second

iphone - 如何在 Interface Builder 中使用 iPhone 主视图之外的 UI 元素设置 UIScrollView?

objective-c - autorelease 与 dealloc 中的 release

ios - "More than maximum 5 filtered albums trying to register. This will fail."使用 Assets 库

iOS 13 `withTintColor` 不服从我分配的颜色