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.
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 :)
in
to separate the parameters from the block body$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)
}
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)
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