【问题标题】:UIView transitionWithView swift syntaxUIView transitionWithView swift语法
【发布时间】:2014-06-25 16:03:01
【问题描述】:

能否帮助我快速了解 transitionWithView 的语法。在objective-c中我会这样使用它:

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

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

【问题讨论】:

标签: ios uiview swift


【解决方案1】:

Swift 5.x 中会是这样的:

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

    // animation

}) { finished in

    // completion

}

【讨论】:

  • 对于未来的人来说,完成块现在可以写成:completion: { finished in //write code here }
【解决方案2】:

我想在接受的答案中补充一点,您可以通过像这样传递数组将多个选项传递给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】:

    斯威夫特 3

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

    【讨论】:

      【解决方案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
          …
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多