gpt4 book ai didi

ios - 傻瓜式自定义函数的 CompletionHandler

转载 作者:行者123 更新时间:2023-11-29 01:27:43 28 4
gpt4 key购买 nike

我在互联网上到处搜索,但无法真正处理我找到的答案。因此,如果有人可以在这里帮助我,我将不胜感激。

我写了一个看起来像这样的函数:

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")
})

现在,我该如何执行作为完成传递的任何内容?
在函数中,在转换的完成 block 中,我尝试了类似

for x in completion {self.x}

但这没有用。

提前致谢:)

最佳答案

例如,您可以像这样在动画 block 的 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")

}
}

关于ios - 傻瓜式自定义函数的 CompletionHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33830467/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com