Lazy instantiating a UIDynamicAnimator with refere

2019-07-19 17:55发布

问题:

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.