UIView transitionWithView swift语法

时间:2014-06-25 16:03:01

标签: ios uiview swift

有些人可以帮助我使用swift中的transitionWithView语法。在objective-c中我会像这样使用它:

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

但是我无法让完成处理程序工作。 感谢

4 个答案:

答案 0 :(得分:22)

Swift 中会是这样的:

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

    // animation

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

    // completion

    })

答案 1 :(得分:10)

我想添加一个已接受的答案,你可以通过传递一个这样的数组将多个选项传递给transitionWithView

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) {

            }
    })

当然你也可以使用这样的完全限定语法:

[UIViewAnimationOptions.CurveEaseOut, UIViewAnimationOptions.TransitionCurlDown]

答案 2 :(得分:7)

Swift 3

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

答案 3 :(得分:4)

the docs找起来有点棘手,但这应该有用:

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

如果您想要completion处理程序,可以使用trailing closure,但在这种情况下,我不会太乱/不清楚。

但是如果你不打算传递一个动画块,它将是边界可读的:

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