ios - 使多个 ScrollView 响应一个取消滚动行为

标签 ios swift uiscrollview

我有一个 View ,里面有四个 ScrollView 。我想要做的是让所有这些 ScrollView 在滚动其中一个时做出响应。我试图通过这段代码做到这一点:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.hpcScrollView.setContentOffset(scrollView.contentOffset, animated: true)
        self.parScrollView.setContentOffset(scrollView.contentOffset, animated: true)
        self.scoreScrollView.setContentOffset(scrollView.contentOffset, animated: true)
        self.holeScrollView.setContentOffset(scrollView.contentOffset, animated: true)
}

但是,当我应用它时,滚动的“行为”被取消了。它仍然滚动,但没有反弹,当我的手指离开 ScrollView 时,它立即停止滚动(见下面的 gif)

enter image description here

有没有办法让它感觉像正常滚动?

最佳答案

不要将 contentOffset 应用于当前正在滚动的 scrollView。在当前滚动的 View 上更改 contentOffset 会导致它停止滚动并立即跳转到偏移量。

let scrollableViews = [hpcScrollView, parScrollView, scoreScrollView, holeScrollView]    

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    scrollableViews.forEach { if $0 != scrollView { $0.setContentOffset(scrollView.contentOffset, animated: true) } }
}

编辑:

每当我们设置内容偏移时,就会调用 scrollViewDidScroll,这会导致 gif 中显示的奇怪滚动行为。

试试这个

let scrollableViews = [hpcScrollView, parScrollView, scoreScrollView, holeScrollView]    

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    scrollableViews.forEach { if $0 != scrollView {
        let scrollBounds = $0.bounds
        scrollBounds.origin = scrollView.contentOffset
        $0.bounds = scrollBounds
    } }
}

关于ios - 使多个 ScrollView 响应一个取消滚动行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48459327/

相关文章:

ios - 对 React Native App 的离线支持

ios - 当用户向下滚动时, Material float 操作按钮应不可见,向上滚动时应可见

ios - 如何在 Swift 中将 UITextField 中的数据用作数组?

uiscrollview - 将 UIScrollView 的底部淡化为透明

iphone - 如何将 UIControl 添加到 UIScrollView contentView?

iOS Cordova : Does Cordova create multiple instances of an objective-C plugin when called more than once?

swift - 使用 objective-c++ 中的 swift (4.0) 类

ios - 使用核心图像进行高效下采样

从 URL 到 UITextlabel 的 Swift4 常量

iphone - 为什么我的 UIScrollView 的 contentSize 没有改变?