![](/img/trans.png)
[英]The completion block of UIView animation gets called before the animation is finished
[英]before completion of UIView animation executing code in completion block
在按鈕上單擊我要旋轉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)
}
}
})
將動畫添加到圖層沒有等待時間您需要完全在動畫塊內部制作動畫邏輯,例如更改幀或執行此操作
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)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.