Lazy instantiating a UIDynamicAnimator with referenceView - Swift

↘锁芯ラ 提交于 2020-01-15 05:51:49

问题


I'm trying to lazy instantiate a UIDynamicAnimator in my code. I want to initialise it with an UIView as an IBOutlet I have.

class ViewController: UIViewController {

     @IBOutlet var gameView : UIView

     @lazy var animator = UIDynamicAnimator(referenceView: gameView)
...

I'm assuming this isn't working because the UIView isn't yet created because the animator property throws back an error saying it can't find the gameView.

How can I get around this?


回答1:


In the session video "Building Interruptible and Responsive Interactions", they did exactly this.

The solution is to define animator as an Optional and initialize it inside viewDidLoad.

class ViewController: UIViewController {
    var animator : UIDynamicAnimator?
    @IBOutlet var gameView : UIView
    override func viewDidLoad() {
       super.viewDidLoad()
       animator = UIDynamicAnimator(referenceView: gameView)
       animator!.property = value
       ...
    }
...

I slightly dislike this because future references to animator will all need to unwrap it.

A slightly better approach imo is to define it as an implicitly unwrapped optional. So it would look like this:

class ViewController: UIViewController {
    var animator : UIDynamicAnimator!
    @IBOutlet var gameView : UIView
    override func viewDidLoad() {
       super.viewDidLoad()
       animator = UIDynamicAnimator(referenceView: gameView)
       animator.property = value
       ...
    }
...



回答2:


Use a closure:

lazy private var dynamo: UIDynamicAnimator = {
        return UIDynamicAnimator(referenceView: self.view)
    }()

"Lazy" means it won't be initialized until first use.

The closure has access to self.view (or whatever other local view you want to use,) but it doesn't try to read it until the first read of "dynamo" happens.

I like this as a combination of clean and safe.



来源:https://stackoverflow.com/questions/24303062/lazy-instantiating-a-uidynamicanimator-with-referenceview-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!