gpt4 book ai didi

ios - Swift:如何重写 scrollView 的方法?

转载 作者:搜寻专家 更新时间:2023-11-01 07:18:19 26 4
gpt4 key购买 nike

我正在尝试为 ScrollView 的子类覆盖 touchesShouldCancel 方法。

这是我的子类代码:

class MyScrollView: UIScrollView {
override func touchesShouldCancel(in view: UIView) -> Bool {
print("Works")
return false
}
}

我制作了这个类的 ScrollView ,它滚动它只是永远不会打印“作品”。我想为此添加一些功能,但它永远不会被调用。根据苹果文档:

“ ScrollView 在开始向内容 View 发送跟踪消息后立即调用此方法。”

这让我相信每当我滚动时都应该调用该方法,但我不明白为什么不是这样。如果有人有任何想法,请干杯。

最佳答案

来自docs :

Because a scroll view has no scroll bars, it must know whether a touch signals an intent to scroll versus an intent to track a subview in the content. To make this determination, it temporarily intercepts a touch-down event by starting a timer and, before the timer fires, seeing if the touching finger makes any movement. If the timer fires without a significant change in position, the scroll view sends tracking events to the touched subview of the content view. If the user then drags their finger far enough before the timer elapses, the scroll view cancels any tracking in the subview and performs the scrolling itself. Subclasses can override the touchesShouldBegin(_:with:in:), isPagingEnabled, and touchesShouldCancel(in:) methods (which are called by the scroll view) to affect how the scroll view handles scrolling gestures.

做这个实验。

  1. 向您的 ScrollView 添加一个拦截触摸的 subview (想想 UIButton,而不是 UILabel)。
  2. 确保您的内容实际上是可滚动的
  3. 按下按钮并稍等片刻,然后开始滑动手指。
    • 当您点击 ScrollView 并立即开始拖动时,它假定您想要滚动 ScrollView 而不是与按钮交互。
    • 但是,当您稍作停顿时,它想要确定您是在尝试与 ScrollView 还是与按钮进行交互。
  4. touchesShouldCancel 将触发。

关于ios - Swift:如何重写 scrollView 的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40617088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com