繁体   English   中英

UIView transitionWithView swift 语法

[英]UIView transitionWithView swift syntax

可以帮助我快速使用 transitionWithView 的语法吗? 在objective-c中,我会像这样使用它:

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

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

Swift 5.x 中会是这样:

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

    // animation

}) { finished in

    // completion

}

我想添加到已接受的答案中,您可以通过传递如下数组将多个选项传递给transitionWithView

斯威夫特 3、4、5

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

           }
   })

斯威夫特 2.0

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

            }
    })

斯威夫特 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]

斯威夫特 3

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

文档有点难找,但这应该有效:

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

如果您想要completion处理程序,您可以使用尾随闭包,但在这种情况下我不会,它会太混乱/不清楚。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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