我是一个菜鸟,一直在从 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
参数的文档摘录指出:
This block has no return value and takes a single Boolean argument that indicates whether or not the animations actually finished before the completion handler was called.
更典型的代码编写方式如下:
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
})
您的用法只是省略了参数周围的括号,并且省去了换行符。将这些重新添加进去可以使代码更清晰。
关于ios - 'finish in' 在 Swift 中是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45250186/