[英]What does 'finish in' mean in Swift?
我是一个菜鸟,一直在学习Apple的Playgrounds和随机书籍做教程。 我正在编写一个处理闭包的教程。 我之前在另一个教程中看到过这个'完成'但我不知道它在外行术语中意味着什么。
什么是完成,什么是完成,内部是什么? 或者是否有操作顺序的想法?
以下是使用它的功能:
func playSequence(index: Int, highlightTime: Double){
currentPlayer = .Computer
if index == inputs.count{
currentPlayer = .Human
return
}
var button: UIButton = buttonByColor(color: inputs[index])
var originalColor: UIColor? = button.backgroundColor
var highlightColor: UIColor = UIColor.white
UIView.animate(withDuration: highlightTime, delay: 0.0, options: [.curveLinear, .allowUserInteraction, .beginFromCurrentState], animations: {
button.backgroundColor = highlightColor
}, completion: {
finished in button.backgroundColor = originalColor
var newIndex: Int = index + 1
self.playSequence(index: newIndex, highlightTime: highlightTime)
})
}
finished
是completion
闭包的参数。 in
只是Swift的闭包语法的一部分。
UIView animate
方法的完整签名是:
class func animate(withDuration duration:TimeInterval,delay:TimeInterval,options:UIViewAnimationOptions = [],animations:@escaping() - > Void,completion:((Bool) - > Void)?= nil)
注意completion
闭包的Bool
参数。 代码中已finished
是为该参数指定的名称。
有关completion
参数的文档摘录如下:
此块没有返回值,并且接受一个布尔参数,该参数指示在调用完成处理程序之前动画是否实际完成。
编写代码的更典型方法如下:
UIView.animate(withDuration: highlightTime, delay: 0.0, options: [.curveLinear, .allowUserInteraction, .beginFromCurrentState], animations: {
// animation code
}) { (finished) in
// completion code
}
此语法使其比您使用的语法更清晰。 这也是使用“尾随闭包”语法。
另一种方式,更接近您的使用,将是:
UIView.animate(withDuration: highlightTime, delay: 0.0, options: [.curveLinear, .allowUserInteraction, .beginFromCurrentState], animations: {
// animation code
}, completion: { (finished) in
// completion code
})
您的用法只是省略了参数周围的括号,并且省略了换行符。 添加这些使代码更清晰。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.