尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

如何给ScrollingStackViewController定制弹性动画:覆盖animate与scrollAnimate闭包的完整指南

如何给ScrollingStackViewController定制弹性动画:覆盖animate与scrollAnimate闭包的完整指南 如何给ScrollingStackViewController定制弹性动画覆盖animate与scrollAnimate闭包的完整指南【免费下载链接】ScrollingStackViewControllerA view controller that uses root views of child view controllers as views in a UIStackView.项目地址: https://gitcode.com/gh_mirrors/sc/ScrollingStackViewControllerScrollingStackViewController 是 iOS 上一个轻量的滚动容器控件它把子视图控制器的根视图放进UIStackView再用UIScrollView实现滚动非常适合“少量、动态、富内容分区”的页面。刚集成时默认的过渡动画偏“硬”很多同学想把它调成更有弹性的效果——核心就在于本文的主角覆盖animate与scrollAnimate两个公开闭包几行代码就能换掉整套动画。一图看懂两个动画闭包各管什么ScrollingStackViewController 把“分区显示/隐藏动画”和“滚动动画”拆成了两个独立可覆盖的闭包这是定制弹性动画的全部入口闭包默认参数生效于哪些方法animate0.5 秒弹簧damping 1无回弹show/hide/set(_:hidden:animated:)以及带动画的removescrollAnimate0.75 秒弹簧damping 0.7初始速度 0.25scrollTo(viewController:) 两个默认实现defaultAnimate、defaultScrollAnimate都定义在ScrollingStackViewController/Classes/ScrollingStackViewController.swift第 49–77 行而animate和scrollAnimate只是两个公开属性初始值指向默认实现——所以随时可以直接赋值替换。两个闭包的签名一致传入“要执行的动画块”和一个completion回调Bool表示动画是否完成你只需要用自己喜欢的UIView.animate(...)方式把它们跑起来。覆盖 animate给分区显示/隐藏加上弹性回弹想让show(vc)/hide(vc)出现“boing”的回弹感把dampingRatio调到 1 以下即可animate { animations, completion in UIView.animate(withDuration: 0.8, dampingRatio: 0.6, initialSpringVelocity: 0.5, options: [], animations: animations, completion: completion) }参数提示dampingRatio 1表示完全不回弹数值越小弹跳越明显。initialSpringVelocity越大起手越有“甩出去”的感觉。覆盖 scrollAnimate换掉“滚动到指定分区”的动画程序化滚动点击后跳到第 N 个分区走的是scrollAnimate改animate对它无效scrollAnimate { animations, completion in UIView.animate(withDuration: 1.0, dampingRatio: 0.7, initialSpringVelocity: 0.25, options: [], animations: animations, completion: completion) }如果只想简单改时长官方 README 给的最简写法也完全够用scrollAnimate { animations, completion in UIView.animate(withDuration: 1, animations: animations, completion: completion) }弹性动画参数建议与覆盖时机参数弹性手感建议区间说明duration0.6 ~ 1.0 秒太短会让回弹显得生硬dampingRatio0.5 ~ 0.7越接近 1 回弹越弱initialSpringVelocity0.25 ~ 0.5决定起始的“冲刺感”✅推荐覆盖时机在你继承 ScrollingStackViewController 的子类viewDidLoad()里、super.viewDidLoad()之后赋值然后再add(viewController:)添加分区override func viewDidLoad() { super.viewDidLoad() animate { animations, completion in UIView.animate(withDuration: 0.8, dampingRatio: 0.6, initialSpringVelocity: 0.5, options: [], animations: animations, completion: completion) } scrollAnimate { animations, completion in UIView.animate(withDuration: 1.0, dampingRatio: 0.7, initialSpringVelocity: 0.25, options: [], animations: animations, completion: completion) } add(viewController: child1) }官方示例工程Example/ScrollingStackViewController/ViewController.swift展示了添加分区、设置spacingColor、调用scrollTo的完整用法可对照阅读。常见坑与排查清单⚠️show/hide传animated: false时不会走animate而是直接改alpha和isHidden——看不出动画效果时先检查这里。⚠️scrollTo只认scrollAnimate只改animate不会让滚动变弹。scrollTo在内容高度尚未确定时会自动延迟到布局完成后再执行不需要手动强制 layout。 闭包第二个参数completion(Bool)用于判断动画是否真正完成适合串联“滚动到位后再高亮”等后续逻辑。相关资料速查核心实现闭包定义与scrollTo逻辑ScrollingStackViewController/Classes/ScrollingStackViewController.swift可运行的演示子类Example/ScrollingStackViewController/ViewController.swift用法与安装说明含动画覆盖示例README.md【免费下载链接】ScrollingStackViewControllerA view controller that uses root views of child view controllers as views in a UIStackView.项目地址: https://gitcode.com/gh_mirrors/sc/ScrollingStackViewController创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表