iOS15下iPhone12系列CollectionView setContentOffset定时分页动画异常
问题根因
这是iOS15.0~15.1版本针对A14芯片(iPhone12系列搭载)优化UIKit渲染逻辑时出现的已知bug,当多个滚动视图短时间内连续触发带系统内置动画的contentOffset变更时,会出现动画调度优先级被压低、动画丢失卡顿的问题,仅在该版本+该芯片组合的设备上复现。
可行解决方案
按优先级尝试以下方案即可修复:
- 替换系统内置动画为显式UIView动画,避免系统动画调度冲突
将原有setContentOffset调用逻辑替换为以下实现:
@objc func rolling1() { DispatchQueue.main.async { self.offset1 = CGPoint(x: self.offset1.x + UIScreen.main.bounds.width, y: self.offset1.y) // 先确保布局稳定 self.collectionView?.layoutIfNeeded() // 用显式动画控制滚动 UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut) { self.collectionView?.setContentOffset(self.offset1, animated: false) } } }
- 调整定时器的RunLoop模式,避免定时器被其他UI事件阻塞
创建定时器后添加到主RunLoop的common模式:
func startTimer(interval: CGFloat) { let timer1 = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(rolling1), userInfo: nil, repeats: true) RunLoop.main.add(timer1, forMode: .common) }
- 关闭CollectionView的预加载功能,降低GPU占用
初始化CollectionView时添加如下配置:
collectionView.isPrefetchingEnabled = false
该问题在iOS15.2及以上版本已经被官方修复,若允许用户升级系统可直接通过升级解决。
内容的提问来源于stack exchange,提问作者SKWyverns29
相关产品推荐
相关产品推荐

