![](/img/trans.png)
[英]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.