我有一個在視圖上執行動畫的函數。我想爲這個函數實現一個完成處理程序,它將在動畫完成後調用。將函數傳遞給完成處理程序
在視圖控制器...
hudView.hide(animated: true, myCompletionHandler: {
// Animation is complete
})
在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
}
}
我已經嘗試了一些東西,但無法找到正確的語法:
}, 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
作爲一個迅速新手這些錯誤消息並不意味着很多,我和我似乎無法找到正確的方式做任何的例子。
將myCompletionHandler
傳遞給.animate
完成處理程序的正確方法是什麼?
謝謝大衛。這很好。 – Turnip