我在互联网上到处搜索,但无法真正处理我找到的答案。因此,如果有人可以在这里帮助我,我们将不胜感激。
我写了一个看起来像这样的函数:
func setImage(imageName: String, completion: ((String) -> Void)?) {
UIView.transitionWithView(self.myImageView, duration: 0.3, options: .CurveEaseOut, animations: {
self.lockImageView.image = UIImage(named: "\(imageName).png")
}, completion: { finished in
//execute the completionBlock that was passed
})
}
我称之为:
setImage("lockCheck", completion: { finished in
print("done")
})
现在,如何执行完成时传递的内容? 在函数中,在转换的完成块中,我尝试了类似
的内容for x in completion {self.x}
但这没有用。
提前致谢:)
答案 0 :(得分:1)
例如,您可以在动画块的completionHandler中立即执行完成处理程序,如下所示:
func setImage(imageName: String, completion: ((Bool) -> Void)?) {
UIView.transitionWithView(self.myImageView, duration: 0.3, options: .CurveEaseOut, animations: { () -> Void in
self.lockImageView.image = UIImage(named: "\(imageName).png")
}, completion: completion)
}
您还可以使用以下额外参数运行完成处理程序(我希望它很清楚):
func setImage(imageName: String, completion: ((Bool, String) -> Void)?) {
UIView.transitionWithView(self.lockImageView, duration: 0.3, options: .CurveEaseOut, animations: { () -> Void in
self.lockImageView.image = UIImage(named: "\(imageName).png")
}) { (finished) -> Void in
// Do some things for example print
print("Hi, this is the animation completion handler")
// Notice the ? because the completion handler is an optional
completion?(finished, "some string")
}
}