UIView transitionWithView swift syntax

2019-01-23 14:40发布

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

标签: ios uiview swift
4条回答
戒情不戒烟
2楼-- · 2019-01-23 15:19

Swift 3

UIView.transition(with: someview,
                                    duration:0.6,
                                    options:UIViewAnimationOptions.transitionFlipFromLeft,
               animations: {
                   // do something
           }, completion:{
               finished in
               // do something
           })
查看更多
Animai°情兽
3楼-- · 2019-01-23 15:22

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
    …
}
查看更多
看我几分像从前
4楼-- · 2019-01-23 15:29

it would be like this in Swift:

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

    // animation

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

    // completion

    })
查看更多
Emotional °昔
5楼-- · 2019-01-23 15:30

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]
查看更多
登录 后发表回答