我试图让属性动画师在呈现视图控制器时启动动画。现在动画正在播放,但是UIViewPropertyAnimator
不响应添加到它的完成处理程序。
UIVisualEffectView
子类。
import UIKit
final class BlurEffectView: UIVisualEffectView {
deinit {
animator?.stopAnimation(true)
}
override func draw(_ rect: CGRect) {
super.draw(rect)
effect = nil
animator = UIViewPropertyAnimator(duration: 1, curve: .linear) { [unowned self] in
self.effect = theEffect
}
animator?.pausesOnCompletion = true
}
private let theEffect: UIVisualEffect = UIBlurEffect(style: .regular)
var animator: UIViewPropertyAnimator?
}
- 第一个视图控制器
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func doSomething(_ sender: UIButton) {
let vc = storyboard?.instantiateViewController(identifier: "second") as! SecondVC
vc.modalPresentationStyle = .overFullScreen
present(vc, animated: false) { //vc presentation completion handler
//adding a completion handler to the UIViewPropertyAnimator
vc.blurView.animator?.addCompletion({ (pos) in
print("animation complete") //the problem is that this line isn't executed
})
vc.blurView.animator?.startAnimation()
}
}
}
- 第二个视图控制器
import UIKit
class SecondVC: UIViewController {
@IBOutlet weak var blurView: BlurEffectView!
override func viewDidLoad() {
super.viewDidLoad()
}
}
这里UIViewPropertyAnimator
完成处理程序是在第二个视图控制器(具有视觉效果视图的控制器)出现之后添加的。我曾尝试将完成处理程序移动到不同的地方viewDidLoad
,viewDidAppear
但似乎没有任何效果。