Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call animateAlongsideTransition in Swift?

Tags:

ios

swift

ios8

I've tried so many combinations in order to call animateAlongSideTransition in Swift for a transition coordinator. I feel like I'm missing something very stupid.

If I want to call this (from Swift docs):

func animateAlongsideTransition(_ animation: ((UIViewControllerTransitionCoordinatorContext!) -> Void)!,
                     completion completion: ((UIViewControllerTransitionCoordinatorContext!) -> Void)!) -> Bool

How would I do it? I just want to pass some things in the animation block and nothing in the completion block.

like image 200
Doug Smith Avatar asked Jun 27 '14 21:06

Doug Smith


2 Answers

This is definitely what you want to do:

coordinator.animateAlongsideTransition({ context in
        // do whatever with your context
        context.viewControllerForKey(UITransitionContextFromViewControllerKey)
    }, completion: nil)

You can also omit the parameters if you use variables like $0 for the first implicit parameter and so

coordinator.animateAlongsideTransition({
        $0.viewControllerForKey(UITransitionContextFromViewControllerKey)
    }, completion: nil)

The in syntax surprises at first, but you have to learn it only once :)

  • The curly brackets defines the block inside the function
  • You use in to separate the parameters from the block body
  • But as I said above, you can omit the parameters by using $0, $1, $2 and so...

It seems that there's a more verbose syntax, but it definitely not fits the Swift spirit (and I'm too lazy to post it there)

Hope it helps (and I don't forget anything...)

Edit:

Another pro tip is when the block is the only parameter, you can even omit the parentheses
(The next will not work, but it's to figure the idea)

coordinator.animateAlongsideTransition{
        $0.viewControllerForKey(UITransitionContextFromViewControllerKey)
    }
like image 149
Crazyrems Avatar answered Nov 12 '22 19:11

Crazyrems


You do it like this (at least in Swift 1.2):

transitionCoordinator.animateAlongsideTransition({context in //things to animate, for example: view.alpha = 0.5
}, completion: nil)
like image 3
Tuslareb Avatar answered Nov 12 '22 19:11

Tuslareb