ios Swift 上的块 (animateWithDuration:animations:completion:)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24071334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Blocks on Swift (animateWithDuration:animations:completion:)
提问by manolosavi
I'm having trouble making the blocks work on Swift. Here's an example that worked (without completion block):
我在使块在 Swift 上工作时遇到问题。这是一个有效的示例(没有完成块):
UIView.animateWithDuration(0.07) {
self.someButton.alpha = 1
}
or alternatively without the trailing closure:
或者没有尾随闭包:
UIView.animateWithDuration(0.2, animations: {
self.someButton.alpha = 1
})
but once I try to add the completion block it just won't work:
但是一旦我尝试添加完成块它就行不通了:
UIView.animateWithDuration(0.2, animations: {
self.blurBg.alpha = 1
}, completion: {
self.blurBg.hidden = true
})
The autocomplete gives me completion: ((Bool) -> Void)?
but not sure how to make it work. Also tried with trailing closure but got the same error:
自动完成给了我,completion: ((Bool) -> Void)?
但不知道如何使它工作。也尝试使用尾随闭包,但得到了同样的错误:
! Could not find an overload for 'animateWithDuration that accepts the supplied arguments
! Could not find an overload for 'animateWithDuration that accepts the supplied arguments
Update for Swift 3 / 4:
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#>
})
I don't use the trailing closure for the completion block because I think it lacks clarity, but if you like it then you can see Trevor's answer below.
我不为完成块使用尾随闭包,因为我认为它不够清晰,但如果你喜欢它,那么你可以在下面看到Trevor 的答案。
回答by Zaksoup
the completion parameter in animateWithDuration takes a block which takes one boolean parameter. In swift, like in Obj C blocks, you must specify the parameters that a closure takes:
animateWithDuration 中的完成参数采用一个带有一个布尔参数的块。在 swift 中,就像在 Obj C 块中一样,您必须指定闭包采用的参数:
UIView.animateWithDuration(0.2, animations: {
self.blurBg.alpha = 1
}, completion: {
(value: Bool) in
self.blurBg.hidden = true
})
The important part here is the (value: Bool) in
. That tells the compiler that this closure takes a Bool labeled 'value' and returns void.
这里的重要部分是(value: Bool) in
. 这告诉编译器这个闭包采用标记为“值”的 Bool 并返回 void。
For reference, if you wanted to write a closure that returned a bool the syntax would be
作为参考,如果您想编写一个返回 bool 的闭包,语法将是
{(value: Bool) -> bool in
//your stuff
}
回答by Nicholas H.
The completion is correct, the closure must accept a Bool
parameter: (Bool) -> ()
. Try
完成是正确的,闭包必须接受一个Bool
参数:(Bool) -> ()
。尝试
UIView.animate(withDuration: 0.2, animations: {
self.blurBg.alpha = 1
}, completion: { finished in
self.blurBg.hidden = true
})
回答by Dan Greenfield
Underscore by itself alongside the in
keyword will ignore the input
在in
关键字旁边单独下划线将忽略输入
Swift 2
斯威夫特 2
UIView.animateWithDuration(0.2, animations: {
self.blurBg.alpha = 1
}, completion: { _ in
self.blurBg.hidden = true
})
Swift 3, 4, 5
斯威夫特 3、4、5
UIView.animate(withDuration: 0.2, animations: {
self.blurBg.alpha = 1
}, completion: { _ in
self.blurBg.isHidden = true
})
回答by Jaro
There is my solution above based on accepted answer above. It fades out a view and hiddes it once almost invisible.
基于上面接受的答案,上面有我的解决方案。它淡出一个视图并将它隐藏起来,一旦几乎不可见。
Swift 2
斯威夫特 2
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
})
}
Swift 3, 4, 5
斯威夫特 3、4、5
func animateOut(view: UIView) {
UIView.animate(withDuration: 0.25, delay: 0.0, options: UIView.AnimationOptions.curveLinear ,animations: {
view.layer.opacity = 0.1
}, completion: { _ in
view.isHidden = true
})
}
回答by Kris Gellci
Here you go, this will compile
给你,这将编译
Swift 2
斯威夫特 2
UIView.animateWithDuration(0.3, animations: {
self.blurBg.alpha = 1
}, completion: {(_) -> Void in
self.blurBg.hidden = true
})
Swift 3, 4, 5
斯威夫特 3、4、5
UIView.animate(withDuration: 0.3, animations: {
self.blurBg.alpha = 1
}, completion: {(_) -> Void in
self.blurBg.isHidden = true
})
The reason I made the Bool area an underscore is because you not using that value, if you need it you can replace the (_) with (value : Bool)
我将 Bool 区域设为下划线的原因是因为您没有使用该值,如果需要,可以将 (_) 替换为 (value : Bool)
回答by Dan Rosenstark
Sometimes you want to throw this in a variable to animate in different ways depending on the situation. For that you need
有时你想把它放在一个变量中,根据情况以不同的方式制作动画。为此你需要
let completionBlock : (Bool) -> () = { _ in
}
Or you could use the equally verbose:
或者你可以使用同样冗长的:
let completionBlock = { (_:Bool) in
}
But in any case, you have have to indicate the Bool
somewhere.
但无论如何,你必须指明Bool
某处。
回答by Trevor
SWIFT 3.x + 4.x
SWIFT 3.x + 4.x
I'd like to make an update and simplify the things.
我想进行更新并简化事情。
Example below is implemented in any view
it is hiding slowly and when it is completely transparent; removes it self from parent view
下面的例子是在任何view
缓慢隐藏和完全透明的情况下实现的;从父级中删除它自己view
ok
variable will always returns true
with animation termination.
ok
变量将始终true
随着动画终止而返回。
alpha = 1
UIView.animate(withDuration: 0.5, animations: {
self.alpha = 0
}) { (ok) in
print("Ended \(ok)")
self.removeFromSuperview()
}