阻塞在 Swift 上(animateWith持续时间: animations: complete:)

在 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#>
})

我没有为完成块使用尾随闭包,因为我认为它缺乏清晰性,但是如果您喜欢它,那么您可以看到 特雷弗的回答如下

89500 次浏览

The completion is correct, the closure must accept a Bool parameter: (Bool) -> (). Try

UIView.animate(withDuration: 0.2, animations: {
self.blurBg.alpha = 1
}, completion: { finished in
self.blurBg.hidden = true
})

Here you go, this will compile

Swift 2

UIView.animateWithDuration(0.3, animations: {
self.blurBg.alpha = 1
}, completion: {(_) -> Void in
self.blurBg.hidden = true
})

Swift 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)

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:

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.

For reference, if you wanted to write a closure that returned a Bool, the syntax would be

{(value: Bool) -> bool in
//your stuff
}

Underscore by itself alongside the in keyword will ignore the input

Swift 2

UIView.animateWithDuration(0.2, animations: {
self.blurBg.alpha = 1
}, completion: { _ in
self.blurBg.hidden = true
})

Swift 3, 4, 5

UIView.animate(withDuration: 0.2, animations: {
self.blurBg.alpha = 1
}, completion: { _ in
self.blurBg.isHidden = true
})

There is my solution above based on accepted answer above. It fades out a view and hiddes it once almost invisible.

Swift 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

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
})
}

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.

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

ok variable will always returns true with animation termination.

    alpha = 1
UIView.animate(withDuration: 0.5, animations: {
self.alpha = 0
}) { (ok) in
print("Ended \(ok)")
self.removeFromSuperview()
}