简体   繁体   中英

UIView transitionWithView swift syntax

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

it would be like this in Swift 5.x :

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

    // animation

}) { finished in

    // completion

}

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]

Swift 3

UIView.transition(with: someview,
                                    duration:0.6,
                                    options:UIViewAnimationOptions.transitionFlipFromLeft,
               animations: {
                   // do something
           }, completion:{
               finished in
               // do something
           })

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
    …
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM