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