man*_*avi 102 closures ios swift
我无法让这些块在Swift上工作.这是一个有效的例子(没有完成块):
UIView.animateWithDuration(0.07) {
self.someButton.alpha = 1
}
Run Code Online (Sandbox Code Playgroud)
或者没有尾随封闭:
UIView.animateWithDuration(0.2, animations: {
self.someButton.alpha = 1
})
Run Code Online (Sandbox Code Playgroud)
但是一旦我尝试添加完成块,它就无法工作:
UIView.animateWithDuration(0.2, animations: {
self.blurBg.alpha = 1
}, completion: {
self.blurBg.hidden = true
})
Run Code Online (Sandbox Code Playgroud)
自动完成功能给了我completion: ((Bool) -> Void)?但不确定如何使它工作.也尝试使用尾随闭包,但得到了同样的错误:
! Could not find an overload for 'animateWithDuration that accepts the supplied arguments
// 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#>
})
Run Code Online (Sandbox Code Playgroud)
小智 200
animateWithDuration中的completion参数采用一个带有一个布尔参数的块.在swift中,就像在Obj C块中一样,您必须指定闭包所采用的参数:
UIView.animateWithDuration(0.2, animations: {
self.blurBg.alpha = 1
}, completion: {
(value: Bool) in
self.blurBg.hidden = true
})
Run Code Online (Sandbox Code Playgroud)
这里的重要部分是(value: Bool) in.这告诉编译器这个闭包采用Bool标记为'value'并返回void.
作为参考,如果你想编写一个返回bool的闭包,那么语法就是
{(value: Bool) -> bool in
//your stuff
}
Run Code Online (Sandbox Code Playgroud)
Nic*_* H. 40
完成是正确的,闭包必须接受一个Bool参数:(Bool) -> ().尝试
UIView.animate(withDuration: 0.2, animations: {
self.blurBg.alpha = 1
}, completion: { finished in
self.blurBg.hidden = true
})
Run Code Online (Sandbox Code Playgroud)
Dan*_*eld 29
与in关键字一起单独下划线会忽略输入
UIView.animateWithDuration(0.2, animations: {
self.blurBg.alpha = 1
}, completion: { _ in
self.blurBg.hidden = true
})
Run Code Online (Sandbox Code Playgroud)
根据上面接受的答案,上面有我的解决方案.它淡出一个视图并隐藏它几乎几乎看不见.
func animateOut(view:UIView) {
UIView.animateWithDuration (0.25, delay: 0.0, options: UIViewAnimationOptions.CurveLinear ,animations: {
view.layer.opacity = 0.1
}, completion: { _ in
view.hidden = true
})
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
83133 次 |
| 最近记录: |