You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现UIScrollView动画滚动且不中断点击高亮功能?

解决自动滚动时无法点击经文高亮的问题

你遇到的核心问题是:UIView默认动画会在执行期间禁用用户交互,导致滚动过程中无法点击经文触发高亮。咱们只需要调整动画的配置选项就能解决这个问题。

解决方案

在UIView.animate的options参数里添加.allowsUserInteraction,让动画执行时仍然允许用户和滚动视图交互。同时,如果你希望点击时暂停滚动(提升用户体验),可以在高亮的点击事件里先停止定时器。

修改后的代码

@objc func move_view() {
    print("moved view")
    self.current_scroll_position += 3
    // 添加.allowsUserInteraction选项,允许动画期间的用户交互
    UIView.animate(withDuration: 0.5, delay: 0, options: [.curveLinear, .allowsUserInteraction], animations: {
        self.scroll_view.contentOffset = CGPoint(x: 0, y: CGFloat(self.current_scroll_position))
    }, completion: nil)
}

// 启动滚动逻辑不变,确保Timer在commonModes下运行(你已经做对了这部分)
print("start scrolling")
self.current_scroll_position = Int(self.scroll_view.contentOffset.y)
self.scroll_timer = Timer.scheduledTimer(timeInterval: TimeInterval(0.5), target: self, selector: #selector(self.move_view), userInfo: nil, repeats: true)
RunLoop.current.add(self.scroll_timer, forMode: .commonModes)
self.is_scrolling = true

额外优化(可选)

如果希望用户点击高亮时暂停滚动,可以在经文的点击回调里添加如下逻辑:

// 假设这是经文的点击事件处理函数
@objc func onVerseTapped(_ sender: UITapGestureRecognizer) {
    if self.is_scrolling {
        self.scroll_timer?.invalidate()
        self.scroll_timer = nil
        self.is_scrolling = false
    }
    // 执行高亮逻辑...
}

这样用户点击时滚动会暂停,高亮完成后你可以根据需求重新启动滚动。

内容的提问来源于stack exchange,提问作者Omar Sinan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:20:37