简体   繁体   中英

Lazy instantiating a UIDynamicAnimator with referenceView - Swift

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?

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
       ...
    }
...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM