UIView transitionWithView swift syntax

2019-01-23 14:58发布

问题:

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

回答1:

it would be like this in Swift:

UIView.transitionWithView(self.view, duration: 0.325, options: UIViewAnimationOptions.CurveEaseInOut, animations: {

    // animation

    }, completion: { (finished: Bool) -> () in

    // completion

    })


回答2:

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

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]


回答3:

Swift 3

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


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


标签: ios uiview swift