【问题标题】:Whats the Swift animate WithDuration syntax?Swift 动画 WithDuration 语法是什么?
【发布时间】:2015-06-23 00:08:01
【问题描述】:

我正在将一个较旧的应用程序移植到 Xcode 7 测试版,但我的动画出现错误:

无法使用类型参数列表调用“animateWithDuration” '(双,延迟:双,选项:无,动画:()-> _, 完成:无)'

代码如下:

 UIView.animateWithDuration(0.5, delay: 0.3, options: nil, animations: {
      self.username.center.x += self.view.bounds.width
    }, completion: nil)

这在 Xcode 6 中有效,所以我假设这是 Swift 中的更新。所以我的问题是:

animateWithDuration 的 Swift 3 语法是什么?

【问题讨论】:

    标签: swift swift2 animatewithduration swift3 optionsettype


    【解决方案1】:

    Swift 3/4 语法

    以下是 Swift 3 语法的更新:

    UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: {
        self.username.center.x += self.view.bounds.width
    }, completion: nil)
    

    如果您需要添加一个完成处理程序,只需像这样添加一个闭包:

    UIView.animate(withDuration: 0.5, delay: 0.3, options: [.repeat, .curveEaseOut, .autoreverse], animations: {
        // animation stuff      
    }, completion: { _ in
        // do stuff once animation is complete
    })
    

    旧答案:

    事实证明这是一个非常简单的修复,只需将options: nil 更改为options: []

    Swift 2.2 语法:

    UIView.animateWithDuration(0.5, delay: 0.3, options: [], animations: {
          self.username.center.x += self.view.bounds.width
        }, completion: nil)
    

    发生了什么变化?

    Swift 2 摆脱了 C 风格的以逗号分隔的选项列表,转而支持选项集(请参阅:OptionSetType)。在我最初的问题中,我为我的选项传递了nil,这在 Swift 2 之前是有效的。使用更新的语法,我们现在看到一个空选项列表作为一个空集:[]

    带有一些选项的 animateWithDuration 示例如下:

     UIView.animateWithDuration(0.5, delay: 0.3, options: [.Repeat, .CurveEaseOut, .Autoreverse], animations: {
          self.username.center.x += self.view.bounds.width
        }, completion: nil)
    

    【讨论】:

    • 他们在 Swift 3 中改变了很多东西,这使得现在学习 Swift 变得非常困难。
    • @ChrisAllinson 我认为这套更改将是最极端的。从这里开始,变化将更加微妙。
    【解决方案2】:

    Swift 3、4、5

    UIView.animate(withDuration: 1.5, delay: 0.05 * Double(index), usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: {
        cell.transform = CGAffineTransform(translationX: 0, y: 0)
    }, completion: nil)
    

    【讨论】:

      【解决方案3】:

      带有完成块的 Swift 3 语法

      UIView.animate(withDuration: 3.0 , delay: 0.25, options: .curveEaseOut, animations: {
      
              // animation
          }, completion: { _ in
      
              // completion
          })
      

      【讨论】:

        【解决方案4】:

        斯威夫特 2

        UIView.animateWithDuration(1.0, delay: 0.1, options: [.Repeat, .CurveEaseOut, .Autoreverse], animations: {
            // animation
        }, completion: { finished in
            // completion
        })
        

        斯威夫特 3、4、5

        UIView.animate(withDuration: 1.0, delay: 0.1, options: [.repeat, .curveEaseOut, .autoreverse], animations: {
            // animation
        }, completion: { finished in
            // completion
        })
        

        【讨论】:

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