Ios 在完成UIView动画之前,在完成块中执行代码
单击按钮后,我想将视图旋转180度。动画后,我想隐藏和显示图像和标签。但隐藏和显示图像和标签的完成代码在动画完成之前执行。检查下面的代码,让我知道我哪里错了吗Ios 在完成UIView动画之前,在完成块中执行代码,ios,swift,uiviewanimation,Ios,Swift,Uiviewanimation,单击按钮后,我想将视图旋转180度。动画后,我想隐藏和显示图像和标签。但隐藏和显示图像和标签的完成代码在动画完成之前执行。检查下面的代码,让我知道我哪里错了吗 var animation = CABasicAnimation(keyPath: "transform.rotation.y") animation.fromValue = NSNumber(value: 0) animation.toValue = NSNumber(value: Double.pi) anim
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)
}
}
})
将动画添加到层没有等待时间,您需要在动画块内完全创建动画逻辑,例如更改帧或执行此操作
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)
}
}