UIView.animateWithDuration(0.2, animations: { () -> Void in
self.layoutIfNeeded()
}) { (finished) -> Void in
.......
.......
}
this code throws this error:
Cannot convert value of type '(_) throws -> Void' to expected argument type '((Bool) -> Void)?'
It seem that you are not closing correctly the parentheses and missing the completion argument.
The most easy way to detect this kind of errors is try to rewrite the method.
Try this:
UIView.animateWithDuration(0.2, animations: { () -> Void in
self.layoutIfNeeded()
}, completion: { (finished) -> Void in
///
})
Write like below code, this will work bcz its working at my side,
UIView.animateWithDuration(0.2, animations: { () -> Void in
//your code here
self.layoutIfNeeded()
}) { (flag : Bool) -> Void in
///
}
This is the code that generates the same xcode, try this.
UIView.animateWithDuration(0.2, animations: {
// Code for animation
}) { (finished: Bool) in
// Code for completion
}
Related
I searched everywhere on the internet but couldn't really deal with the answers I found. So if someone could help me here, that'd be appreciated.
I wrote a function that looks like this:
func setImage(imageName: String, completion: ((String) -> Void)?) {
UIView.transitionWithView(self.myImageView, duration: 0.3, options: .CurveEaseOut, animations: {
self.lockImageView.image = UIImage(named: "\(imageName).png")
}, completion: { finished in
//execute the completionBlock that was passed
})
}
I call it like this:
setImage("lockCheck", completion: { finished in
print("done")
})
Now, how do I execute whatever was passed as completion?
In the function, in the transition's completion block, I tried something like
for x in completion {self.x}
but that didn't work.
Thanks in advance :)
You could for example execute the completion handler right away in the completionHandler of the animation block like so:
func setImage(imageName: String, completion: ((Bool) -> Void)?) {
UIView.transitionWithView(self.myImageView, duration: 0.3, options: .CurveEaseOut, animations: { () -> Void in
self.lockImageView.image = UIImage(named: "\(imageName).png")
}, completion: completion)
}
You can also run an completion handler with extra parameters like the following (I hope it is clear like this):
func setImage(imageName: String, completion: ((Bool, String) -> Void)?) {
UIView.transitionWithView(self.lockImageView, duration: 0.3, options: .CurveEaseOut, animations: { () -> Void in
self.lockImageView.image = UIImage(named: "\(imageName).png")
}) { (finished) -> Void in
// Do some things for example print
print("Hi, this is the animation completion handler")
// Notice the ? because the completion handler is an optional
completion?(finished, "some string")
}
}
In a ViewController in my app I call transitionFromViewController but always get the following error when passing in a closure to the completion: argument.
Type '() -> Void' does not conform to protocol 'NilLiteralConvertible'
Here's the function call:
self.transitionFromViewController(
self.currentVC,
toViewController: newController,
duration: 0.2,
options: UIViewAnimationOptions.TransitionCrossDissolve,
nil,
completion: { finished in
fromViewController.removeFromParentViewController()
toViewController.didMoveToParentViewController(containerViewController)
toViewController.view.frame = containerViewController.view.bounds
})
According to code completion the method signature is as follows:
transitionFromViewController(fromViewController: UIViewController, toViewController: UIViewController, duration: NSTimeInterval, options: UIViewAnimationOptions, animations: () -> Void(), completion: ((Bool) -> Void)?)
You cannot pass nil to animations paramere () -> Void() declared as not optional
Pass empty closure if you want
self.transitionFromViewController(
self.currentVC,
toViewController: newController,
duration: 0.2,
options: UIViewAnimationOptions.TransitionCrossDissolve,
animations: { () -> Void in
},
completion: { finished in
fromViewController.removeFromParentViewController()
toViewController.didMoveToParentViewController(containerViewController)
toViewController.view.frame = containerViewController.view.bounds
})
I think following code should help you :
self.transitionFromViewController(fromViewController, toViewController: toViewController, duration: 0.1, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
// code for animations
}) { (value: Bool) -> Void in
// code after completion
}
Where, // code for animations - code you want to execute during the block.
And, // code after completion - code you want to execute after the completion of the block.
I'm having a hard time understanding a problem I'm having.
To simplify, I'll use UIView method.
Basically, if I write the method
UIView.animateWithDuration(1, animations: {() in
}, completion:{(Bool) in
println("test")
})
it works fine.
Now, if I do the same method, but creating a string like so:
UIView.animateWithDuration(1, animations: {() in
}, completion:{(Bool) in
String(23)
})
It stops working. Compiler error: Missing argument for parameter 'delay' in call
Now, here's the strange part. If I do the exact same code as the one that fails, but just add a print command like so:
UIView.animateWithDuration(1, animations: {() in
}, completion:{(Bool) in
String(23)
println("test")
})
it starts to work again.
My problem is basically the same thing. My code:
downloadImage(filePath, url: url) { () -> Void in
self.delegate?.imageDownloader(self, posterPath: posterPath)
}
Doesn't work. But if I change to.
downloadImage(filePath, url: url) { () -> Void in
self.delegate?.imageDownloader(self, posterPath: posterPath)
println("test")
}
or even:
downloadImage(filePath, url: url) { () -> Void in
self.delegate?.imageDownloader(self, posterPath: posterPath)
self.delegate?.imageDownloader(self, posterPath: posterPath)
}
It works fine.
I can't understand why this is happening. I'm close to accept that it's a compiler bug.
Closures in Swift have implicit returns when they are only made up of a single expression. This allow for succinct code such as this:
reversed = sorted(names, { s1, s2 in s1 > s2 } )
In your case, when you create your string here:
UIView.animateWithDuration(1, animations: {() in }, completion:{(Bool) in
String(23)
})
you end up returning that string and that makes the signature of your closure:
(Bool) -> String
That no longer matches what's required by animateWithDuration's signature (which translates to Swift's cryptic Missing argument for parameter 'delay' in call error because it can't find an appropriate signature to match).
An easy fix is to add an empty return statement at the end of your closure:
UIView.animateWithDuration(1, animations: {() in}, completion:{(Bool) in
String(23)
return
})
Which makes your signature what it should be:
(Bool) -> ()
Your last example:
downloadImage(filePath, url: url) { () -> Void in
self.delegate?.imageDownloader(self, posterPath: posterPath)
self.delegate?.imageDownloader(self, posterPath: posterPath)
}
works because there are two expressions there, not just one; implicit returns only happen when the closure contains a single expression. So, that closure isn't returning anything and that matches its signature.
I am trying to use animateWithDuration closure in Swift. I have declared the arguments in the closure as mentioned in the Apple Book for Swift. However, I am still getting an error.
Below is the code snippet:
if(!isRotating){
isRotating = true
var myImageTemp :UIImageView = self.myImage
UIView.animateWithDuration(0.5, delay: 1, options: UIViewAnimationCurve.EaseOut, animations:
{
() in myImageTemp.transform = CGAffineTransformMakeRotation(angle + M_PI_2)
},
completion:
{
(Bool finished) in self.pathAnimation() })
}
It gives me an error:
Could find an overload that accepts the supplied arguments.
And also it tells me:
Implicit use of self in closure.
Can anybody help me with this?
Just try:
UIView.animateWithDuration(0.2,
animations:
{
// your code.
},
completion:
{
(completed: Bool) in
// your code.
})
The (completed: Bool) in part indicates that the closure takes a Bool parameter labeled completed. If you are not interested in accessing the completed parameter, you can ignore it using an underscore.
UIView.animateWithDuration(0.2,
animations:
{
// your code.
},
completion:
{ _ in
// your code.
})
I'm having trouble making the blocks work on Swift. Here's an example that worked (without completion block):
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:
! Could not find an overload for 'animateWithDuration that accepts the supplied arguments
Update for 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.
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
}
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
})
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
})
}
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)
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()
}