18

Can some help me with the syntax of the transitionWithView in swift. in objective-c I would use it like so:

[UIView transitionWithView:[ self view ] duration:0.325 options:UIViewAnimationOptionCurveEaseOut animations:
 ^{
     // do the transition
 }
 completion:
 ^( BOOL finished ){
    // cleanup after the transition
 }];

However I can not get the completion handler to work. thanks

reza23
  • 3,079
  • 2
  • 27
  • 42
  • 2
    I recommend to read more about closures in _Swift_: https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/Closures.html – holex Jun 25 '14 at 16:10

4 Answers4

26

it would be like this in Swift 5.x:

UIView.transition(with: self.view, duration: 0.325, options: .curveEaseInOut, animations: {

    // animation

}) { finished in

    // completion

}
holex
  • 23,961
  • 7
  • 62
  • 76
  • For those coming in the future, the completion block can now be written as: `completion: { finished in //write code here }` – kbpontius Jun 22 '15 at 17:50
10

I'd like to add to the accepted answer that you can pass multiple options into transitionWithView by passing an array like so:

Swift 3, 4, 5

UIView.transition(with: status, duration: 0.33, options:
       [.curveEaseOut, .transitionCurlDown], animations: {
           //...animations
       }, completion: {_ in
           //....transition completion
           delay(seconds: 2.0) {

           }
   })

Swift 2.0

 UIView.transitionWithView(status, duration: 0.33, options:
        [.CurveEaseOut, .TransitionCurlDown], animations: {
            //...animations
        }, completion: {_ in
            //....transition completion
            delay(seconds: 2.0) {

            }
    })

Swift 1.2

UIView.transitionWithView(status, duration: 0.33, options:
        .CurveEaseOut | .TransitionCurlDown, animations: {
            //...animations
        }, completion: {_ in
            //transition completion
            delay(seconds: 2.0) {

            }
    })

and of course you can use the fully qualified syntax like this as well:

[UIViewAnimationOptions.CurveEaseOut, UIViewAnimationOptions.TransitionCurlDown]
Karen Hovhannisyan
  • 1,140
  • 2
  • 21
  • 31
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
7

Swift 3

UIView.transition(with: someview,
                                    duration:0.6,
                                    options:UIViewAnimationOptions.transitionFlipFromLeft,
               animations: {
                   // do something
           }, completion:{
               finished in
               // do something
           })
cvb
  • 1,715
  • 18
  • 22
4

the docs are a little tricky to find, but this should work:

UIView.transitionWithView(self.view, 
                 duration:0.325,
                  options: options:UIViewAnimationOptions.CurveEaseOut,
               animations: {
                             …
                           },
               completion: {
                             finished in
                             …
                           })

You could use a trailing closure if you wanted for the completion handler, but I wouldn't in this case, it would be too messy/unclear.

But if you weren't going to pass an animations block, it would be borderline readable:

UIView.transitionWithView(self.view, duration:0.325, options: options:UIViewAnimationOptions.CurveEaseOut, animations: nil) {
    finished in
    …
}
Jiaaro
  • 74,485
  • 42
  • 169
  • 190