Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocks on Swift (animateWithDuration:animations:completion:)

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.

like image 336
manolosavi Avatar asked Jun 05 '14 22:06

manolosavi


People also ask

How do I view an animation in Swift?

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.

How do I show an animation label in Swift?

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.

What is animation in Swift?

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.

What is the best way to learn iOS animation blocks?

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.

How do I call a completion closure in Swift?

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.

What is keyframe animation in UIKit?

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.

How to animate a table view with spring animation?

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.


2 Answers

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 }) 
like image 23
Nicholas H. Avatar answered Oct 17 '22 04:10

Nicholas H.


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 } 
like image 71
Zaksoup Avatar answered Oct 17 '22 05:10

Zaksoup