I have a function that performs an animation on a view. I would like to implement a completion handler for this function that would be called after the animation is complete.
In ViewController...
hudView.hide(animated: true, myCompletionHandler: {
// Animation is complete
})
In HudView class...
func hide(animated: Bool, myCompletionHandler: () -> Void) {
if animated {
transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [], animations: {
self.alpha = 0
self.transform = CGAffineTransform.identity
}, completion: nil) // I want to run 'myCompletionHandler' in this completion handler
}
}
I've tried a number of things but can't find the correct syntax:
}, completion: myCompletionHandler)
Passing non-escaping parameter 'myCompletionHandler' to function expecting an @escaping closure
}, completion: myCompletionHandler())
Cannot convert value of type 'Void' to expected argument type '((Bool) -> Void)?'
}, completion: { myCompletionHandler() })
Closure use of non-escaping parameter 'myCompletionHandler' may allow it to escape
As a swift novice these error messages don't mean very much to me and I cant seem to find any examples of the correct way to do this.
What is the correct way to pass myCompletionHandler to the .animate completion handler?