【发布时间】:2014-07-27 02:30:11
【问题描述】:
我无法让这些块在 Swift 上运行。这是一个有效的示例(没有完成块):
UIView.animateWithDuration(0.07) {
self.someButton.alpha = 1
}
或者没有尾随闭包:
UIView.animateWithDuration(0.2, animations: {
self.someButton.alpha = 1
})
但是一旦我尝试添加完成块,它就不起作用了:
UIView.animateWithDuration(0.2, animations: {
self.blurBg.alpha = 1
}, completion: {
self.blurBg.hidden = true
})
自动完成功能给了我completion: ((Bool) -> Void)?,但不知道如何让它工作。也尝试了尾随闭包,但得到了同样的错误:
! Could not find an overload for 'animateWithDuration that accepts the supplied arguments
Swift 3 / 4 更新:
// This is how I do regular animation blocks
UIView.animate(withDuration: 0.2) {
<#code#>
}
// Or with a completion block
UIView.animate(withDuration: 0.2, animations: {
<#code#>
}, completion: { _ in
<#code#>
})
我没有为完成块使用尾随闭包,因为我认为它不够清晰,但如果你喜欢它,那么你可以看到Trevor's answer below。
【问题讨论】:
-
“自动完成功能让我完成:((Bool) -> Void)?但不知道如何使它工作”正是它所说的。这必须是一个接受 Bool 并返回 Void 的块。
-
你怎么知道 ((Bool) -> Void) 是什么?是为了?因为在我知道它是 BOOL 完成之前,我已经在 ObjC 中使用了它。但是一个 swift 编码器怎么知道呢?