[英]Pass function to completion handler
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... 在ViewController中...
hudView.hide(animated: true, myCompletionHandler: {
// Animation is complete
})
In HudView class... 在HudView类中...
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
将非转义参数'myCompletionHandler'传递给需要@转义闭包的函数
}, completion: myCompletionHandler())
Cannot convert value of type 'Void' to expected argument type '((Bool) -> Void)?'
无法将类型'Void'的值转换为期望的参数类型'((Bool)-> Void)?'
}, completion: { myCompletionHandler() })
Closure use of non-escaping parameter 'myCompletionHandler' may allow it to escape
关闭使用非转义参数'myCompletionHandler'可能会使其转义
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? 将
myCompletionHandler
传递给.animate
完成处理程序的正确方法是什么?
If you want to pass your own closure as an input argument to UIView.animate
, you need to match the types of the closures, so myCompletionHandler
has to have a type of ((Bool) -> ())?
如果要将自己的闭包作为输入参数传递给
UIView.animate
,则需要匹配闭包的类型,因此myCompletionHandler
必须具有((Bool) -> ())?
, just like completion
. ,就像
completion
。
func hide(animated: Bool, myCompletionHandler: ((Bool) -> ())?) {
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: myCompletionHandler) // I want to run 'myCompletionHandler' in this completion handler
}
}
This is how you can call it: 您可以这样称呼它:
hudView.hide(animated: true, myCompletionHandler: { success in
//animation is complete
})
This is how to use completion in UIView.animate : 这是在UIView.animate中使用完成的方法:
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: { (success) in
myCompletionHandler()
})
}
}
You can create your function as,
func hide(_ animated:Bool, completionBlock:((Bool) -> Void)?){
}
And you can call it as,
self.hide(true) { (success) in
// callback here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.