问题描述
我在一个页面上有几个 UIScrollView.您可以单独滚动它们或将它们锁定在一起并将它们作为一个滚动.当它们被锁定时会出现问题.
I've got a a few UIScrollView on a page. You can scroll them independently or lock them together and scroll them as one. The problem occurs when they are locked.
我使用 UIScrollViewDelegate 和 scrollViewDidScroll: 来跟踪运动.我查询 UIScrollView 的 contentOffset 已更改,然后通过将其 contentOffset 属性设置为匹配来反映更改到其他滚动视图.
I use UIScrollViewDelegate and scrollViewDidScroll: to track movement. I query the contentOffset of the UIScrollView which changed and then reflect change to other scroll views by setting their contentOffset property to match.
太好了....除了我注意到很多额外的电话.以编程方式更改滚动视图的 contentOffset 会触发委托方法 scrollViewDidScroll: 被调用.我尝试使用 setContentOffset:animated: 代替,但我仍然在委托上获得触发器.
Great.... except I noticed a lot of extra calls. Programmatically changing the contentOffset of my scroll views triggers the delegate method scrollViewDidScroll: to be called. I've tried using setContentOffset:animated: instead, but I'm still getting the trigger on the delegate.
如何以编程方式修改我的 contentOffsets 以不触发 scrollViewDidScroll:?
How can I modify my contentOffsets programmatically to not trigger scrollViewDidScroll:?
实施说明....每个 UIScrollView 都是自定义 UIView 的一部分,它使用委托模式回调处理协调各种 contentOffset 的呈现 UIViewController 子类 值.
Implementation notes.... Each UIScrollView is part of a custom UIView which uses delegate pattern to call back to the presenting UIViewController subclass that handles coordinating the various contentOffset values.
推荐答案
可以在不触发委托回调scrollViewDidScroll:的情况下改变UIScrollView的内容偏移量,通过设置 UIScrollView 的边界并将原点设置为所需的内容偏移量.
It is possible to change the content offset of a UIScrollView without triggering the delegate callback scrollViewDidScroll:, by setting the bounds of the UIScrollView with the origin set to the desired content offset.
CGRect scrollBounds = scrollView.bounds; scrollBounds.origin = desiredContentOffset; scrollView.bounds = scrollBounds;