[英]How to Apply Animation while changing UIView size? + Swift 5
[英]Swift: how to finish a UIView draw animation before changing a label
關於 animation 的時序,我有一點問題。
順序應該是:
實際上順序是:
下面是主VC的代碼:
import Foundation
import UIKit
class MainVC: UIViewController {
var counter: Int = 0
@IBOutlet weak var counterLabel: UILabel!
@IBOutlet weak var nextButton: UIButton!
@IBOutlet weak var animationView: AnimationView!
@IBAction func nextButtonPressed(_ sender: UIButton) {
counter += 1
animationView.progress = 1
//this view draws a progress bar and animates it from 0 to 100% via CABasicAnimation.
counterLabel.text = "\(counter)"
animationView.progress = 0
//the progress bar is reset
}
override func viewDidLoad() {
super.viewDidLoad()
nextButton.setTitle("Next")
counterLabel.text = "\(counter)"
}
}
我已經嘗試過調度隊列,但我無法讓它正常工作。 關於如何解決這個問題的任何想法?
您沒有顯示足夠多的代碼,我們無法專門為您提供幫助。
一般來說,你會設置一些 object 作為你的 CAAnimation 的代理,並實現方法func animationDidStop(_ anim: CAAnimation, finished flag: Bool)
。
在該方法中,您將更改 label 文本(假設finished == true
。)
我不太喜歡動畫的委托模式。 如果您有多個動畫,為每個動畫處理自定義完成代碼可能會很痛苦。 我所做的是使用您可以將對象附加到 CAAnimation 對象,並將閉包附加到 animation 的事實。 然后我將視圖 controller 設置為委托,並在視圖控制器的animationDidStop()
中,查找附加到 animation 的閉包,如果有,則執行它。
有關使用鍵值編碼將對象附加到CAAnimation
和CALayer
對象的討論,請參閱這篇文章:
它很舊,使用 Objective-C 編寫,但 Swift 中的概念相同,非常有用。 有關鍵值編碼的更一般性討論(針對 Swift進行了更新),請參閱這篇文章: https://developer.apple.com/documentation/objectivec/nsobject/nskeyvaluecoding
(Swift中你需要知道的兩個函數是setValue(_:forKeyPath:)
和value(forKey:)
。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.