iOS中如何用Snapkit为FSCalendar切换Scope添加按钮点击动画
解决FSCalendar切换Scope时的动画问题(Snapkit实现)
- 问题根源:当前代码未将布局更新操作放入UI动画闭包中,导致切换视图时无过渡动画
- 修改后的可运行代码:
func calendar(_ calendar: FSCalendar, boundingRectWillChange bounds: CGRect, animated: Bool) { self.calendar.snp.updateConstraints { make in make.height.equalTo(bounds.height) } // 根据FSCalendar传入的animated参数决定是否执行动画 if animated { UIView.animate(withDuration: 0.3) { self.view.layoutIfNeeded() } } else { self.view.layoutIfNeeded() } }
- 补充说明:
- 必须利用回调里的
animated参数,这是FSCalendar内置的动画状态标识 - 只有把
layoutIfNeeded()放在UIView.animate闭包内,才能触发约束变化的平滑过渡 - 可根据需求调整
withDuration的数值自定义动画时长
- 必须利用回调里的
内容的提问来源于stack exchange,提问作者bch7213
相关产品推荐
相关产品推荐

