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
// 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.
To be exact, whenever you want to animate the view, you actually call layoutIfNeeded on the superview of that view. Try this instead: UIView. animate(withDuration: 0.1, delay: 0.1, options: UIViewAnimationOptions.
width will make it appear at the left of the view, so you can't see any effect on it. This will make your label appear from the left to the right. This is the correct way programmatically, but the animateWithDuration should be placed in viewDidAppear to achieve the desired effect.
Animations can add visual cues that notify users about what's going on in the app. In iOS, animations are used extensively to reposition views, change their size, remove them from view hierarchies, and hide them.
UIView’s animation API is one of the most common areas that iOS devs use blocks. Before blindly writing your own method, try using one of Apple’s code samples to familiarize yourself with the syntax.
First is the name of our closure, which is how we will call it later in the method, then the parameter (array of Users), followed by our return value (void). Below is an approximation of what the body of this method looks like: Calling our completion closure in Swift is almost the same way we call our completion block in Objective-C.
Instead of an animation that just moves along a path, UIKit will execute the different stages of the animation. The Keyframe animation APIs are as follows.
We reload the table view data and loop through the cells that are currently visible on the screen and move each of them to the bottom of the screen. We then iterate over all the cells that we moved to the bottom of the screen and animate them back to position with a spring animation. Run the app and you should see the following.
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 })
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With