【发布时间】:2019-11-24 10:27:31
【问题描述】:
CALayer 动画
我的应用程序的目的是在 TableView 中显示 ToDos/Tasks。所有任务都有一个开始值和一个结束值。
对于第一个任务,我想显示一个进度指示器,以便更好地了解剩余时间。对于背景,我的 ViewController 中有一个带有 autoLayout 约束的 UIView。我用于动画的 CALayer 是 backgroundView 的子层。 x 轴上图层的起点是变量,因此即使重新加载应用程序,计时器也设置正确。终点是一个绝对值(背景视图的全宽)。这些参数都可以正常工作,只是动画不起作用。我正在设置动画应该从哪里开始的值,以及剩下的持续时间,但 CALayer 不会移动。我尝试使用 UIView.animate 和 CABasicAnimation 操作图层,但在每种情况下,将图层设置为其起始位置后,动画都不会开始。
(https://ise-technology.com/cdn/stack-overflow/calayertimer/timer)
// func for setting setting the CALayer and the BackgroundView for the given values
func setTimerView(isActive: Bool, color: UIColor, animationDuration: Double){
if isActive == true {
configureBackgroundView(bool: true) // just sets the height for the background view
backgroundTimerView.backgroundColor = color.withAlphaComponent(0.3)
timerView.backgroundColor = color.cgColor
setAnimationForDuration(duration: animationDuration)
} else {
configureBackgroundView(bool: false)
timerView.backgroundColor = UIColor.clear.cgColor
}
}
// func where the animation should be embedded
func setAnimationForDuration(duration: Double) {
let startPoint = setStartPoint(duration: duration)
timerView.frame.origin.x = CGFloat(-startPoint)
// after updating the origin X of the Layer to the current position, its not possible to start an animation. My attempts to here place in this func
let animation = CABasicAnimation()
animation.duration = duration
animation.toValue = timerView.frame.origin.x = 0
timerView.add(animation, forKey: nil)
}
【问题讨论】: