Swift3 忽略UIView动画的持续时间
我遇到了这个棘手的问题,我甚至不知道如何解释它(因为NDA不能显示太多代码) 我的视图有一个自定义元素,一个彩色条,它有一条基于用户滚动表格的曲线。这非常有效,当用户滚动时,调用委托,并调用更新彩色条曲线的方法。因此,曲线将“在循环中”更新 现在我必须按程序移动它。 当动画出现时,彩色条不会设置动画,而是突然变为最终状态 我知道为什么会这样Swift3 忽略UIView动画的持续时间,swift3,uiviewanimation,Swift3,Uiviewanimation,我遇到了这个棘手的问题,我甚至不知道如何解释它(因为NDA不能显示太多代码) 我的视图有一个自定义元素,一个彩色条,它有一条基于用户滚动表格的曲线。这非常有效,当用户滚动时,调用委托,并调用更新彩色条曲线的方法。因此,曲线将“在循环中”更新 现在我必须按程序移动它。 当动画出现时,彩色条不会设置动画,而是突然变为最终状态 我知道为什么会这样 view.layoutIfNeeded() UIView.animate(withDuration: 0.2, animations: {
view.layoutIfNeeded()
UIView.animate(withDuration: 0.2, animations: {
self.headerHeightConstraint.constant = self.maxHeaderHeight
self.updateHeaderAlpha()
self.view.layoutIfNeeded()
}, completion: { _ in
onComplete()
})
将约束值设置为最终值,并且只调用一次updateHeaderAlpha(这一个设置了曲线值)。因此,动画确实发生了,但条形图在开始时变为最终状态
“解决方案”应该是在动画的“每个周期”中调用updateHeader方法,但我不能这样做。我已经试过:
- 添加一个观察者:只是不,这不起作用
- 递归调用该方法:没有动画(或者发生得太快)
- 使用计时器
- 使用睡眠/延迟(是的,对不起)
- 用户在动画中重复并开始当前状态并更改: self.headerheight约束常数=self.maxHeaderHeight
func collapseHeader(_ onComplete: @escaping (()->Void) = {}) {
view.layoutIfNeeded()
UIView.animate(withDuration: 0, animations: {
self.headerHeightConstraint.constant -= 1
self.updateHeaderAlpha()
self.view.layoutIfNeeded()
}, completion: { _ in
if self.headerHeightConstraint.constant <= self.minHeaderHeight + 1 {
onComplete()
} else {
self.collapseHeader() {
onComplete()
}
}
})
}
func collapseHeader(uoncomplete:@escaping(()->Void)={}){
view.layoutIfNeeded()
UIView.animate(持续时间:0,动画:{
自人头重量恒定常数-=1
self.updateHeaderAlpha()
self.view.layoutifneed()
},完成:{uuuin
如果self.headerHeightConstraint.constant,请使用自定义的可设置动画的属性编写自己的图层。周围有很多示例。CAAnimation
是关键。谢谢朋友,这就是我需要的提示
func collapseHeader(_ onComplete: @escaping (()->Void) = {}) {
view.layoutIfNeeded()
UIView.animate(withDuration: 0, animations: {
self.headerHeightConstraint.constant -= 1
self.updateHeaderAlpha()
self.view.layoutIfNeeded()
}, completion: { _ in
if self.headerHeightConstraint.constant <= self.minHeaderHeight + 1 {
onComplete()
} else {
self.collapseHeader() {
onComplete()
}
}
})
}