在按钮上单击“我想将视图旋转180度”。动画之后,我想隐藏并显示图像和标签。但是用于隐藏和显示图像和标签的完成代码在动画完成之前执行。检查以下代码,让我知道我在任何地方都错了吗?
var animation = CABasicAnimation(keyPath: "transform.rotation.y")
animation.fromValue = NSNumber(value: 0)
animation.toValue = NSNumber(value: Double.pi)
animation.repeatCount = 1
animation.duration = 5.0
UIView.animate(withDuration: 5.0, animations: {
self.viewContainer.layer.add(animation, forKey: "rotation")
}, completion: { finished in
if finished {
if self.strInfo == "Image" {
self.strInfo = "Info"
self.lblInfo.isHidden = false
self.imageView.isHidden = true
self.btnInfo.setBackgroundImage(UIImage(named:"close"), for: .normal)
} else if self.strInfo == "Info"{
self.strInfo = "Image"
self.lblInfo.isHidden = true
self.imageView.isHidden = false
self.imageView.image = UIImage(named: self.strPhotoName)
self.btnInfo.setBackgroundImage(UIImage(named:"info"), for: .normal)
}
}
})
答案 0 :(得分:1)
将动画添加到图层没有等待时间,您需要使动画块内部的动画逻辑完全改变,例如更改帧或执行此操作
self.viewContainer.layer.add(animation, forKey: "rotation")
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
if self.strInfo == "Image" {
self.strInfo = "Info"
self.lblInfo.isHidden = false
self.imageView.isHidden = true
self.btnInfo.setBackgroundImage(UIImage(named:"close"), for: .normal)
} else if self.strInfo == "Info"{
self.strInfo = "Image"
self.lblInfo.isHidden = true
self.imageView.isHidden = false
self.viewContainer.backgroundColor = .white
self.imageView.image = UIImage(named: self.strPhotoName)
self.btnInfo.setBackgroundImage(UIImage(named:"info"), for: .normal)
}
}