我有一个列表页面和详细信息页面。在我的列表页面中有一个UITableView。 在我的tableviewcell中:
var buttonStyle: ButtonStyle = .Button {
didSet {
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotationAnimation.toValue = NSNumber(value: Double.pi * 2.0)
rotationAnimation.duration = 2
rotationAnimation.isCumulative = true
rotationAnimation.repeatCount = Float.infinity
rotationAnimation.isRemovedOnCompletion = false
if oldValue != buttonStyle {
switch buttonStyle {
case .Button:
self.loadingView.isHidden = true
self.progressView.isHidden = true
self.opertaionBtn.isHidden = false
self.bringSubview(toFront: self.opertaionBtn)
case .Loading:
//self.loadingView.layer.add(rotationAnimation, forKey: "rotationAnimation")
self.loadingView.isHidden = false
self.progressView.isHidden = true
self.opertaionBtn.isHidden = true
self.bringSubview(toFront: self.loadingView)
}
}
}
}
我以前在didSet上添加了动画。当页面从详细信息页面退回时,我将buttonStyle设置为Loading并更新单元格。但是动画不会移动。所以我写了一个函数来放置动画。
func addAnimation() {
var anime = self.loadingView.layer.animation(forKey: "rotationAnimation") as? CABasicAnimation
if anime == nil {
anime = CABasicAnimation(keyPath: "transform.rotation.z")
anime.toValue = NSNumber(value: Double.pi * 2.0)
anime.duration = 2
anime.isCumulative = true
anime.repeatCount = Float.infinity
anime.isRemovedOnCompletion = false
self.loadingView.layer.add(anime, forKey: "rotationAnimation")
}
}
当返回列表页面时,我首先设置buttonStyle和addAnimation()
。它运作良好。有人知道为什么吗?谢谢!