【问题标题】:Blocks on Swift (animateWithDuration:animations:completion:)Swift 上的块 (animateWithDuration:animations:completion:)
【发布时间】:2014-07-27 02:30:11
【问题描述】:

我无法让这些块在 Swift 上运行。这是一个有效的示例(没有完成块):

UIView.animateWithDuration(0.07) {
    self.someButton.alpha = 1
}

或者没有尾随闭包:

UIView.animateWithDuration(0.2, animations: {
    self.someButton.alpha = 1
})

但是一旦我尝试添加完成块,它就不起作用了:

UIView.animateWithDuration(0.2, animations: {
    self.blurBg.alpha = 1
}, completion: {
    self.blurBg.hidden = true
})

自动完成功能给了我completion: ((Bool) -> Void)?,但不知道如何让它工作。也尝试了尾随闭包,但得到了同样的错误:

! Could not find an overload for 'animateWithDuration that accepts the supplied arguments

Swift 3 / 4 更新:

// This is how I do regular animation blocks
UIView.animate(withDuration: 0.2) {
    <#code#>
}

// Or with a completion block
UIView.animate(withDuration: 0.2, animations: {
    <#code#>
}, completion: { _ in
    <#code#>
})

我没有为完成块使用尾随闭包,因为我认为它不够清晰,但如果你喜欢它,那么你可以看到Trevor's answer below

【问题讨论】:

  • “自动完成功能让我完成:((Bool) -> Void)?但不知道如何使它工作”正是它所说的。这必须是一个接受 Bool 并返回 Void 的块。
  • 你怎么知道 ((Bool) -> Void) 是什么?是为了?因为在我知道它是 BOOL 完成之前,我已经在 ObjC 中使用了它。但是一个 swift 编码器怎么知道呢?

标签: ios swift closures


【解决方案1】:

补全正确,闭包必须接受Bool参数:(Bool) -&gt; ()。试试

UIView.animate(withDuration: 0.2, animations: {
    self.blurBg.alpha = 1
}, completion: { finished in
    self.blurBg.hidden = true
})

【讨论】:

  • 值得注意的是,这个版本正确的。你不需要写出完整的类型,因为它可以被推断出来,所以你只需要提到这里提到的闭包参数。
【解决方案2】:

你去,这将编译

斯威夫特 2

UIView.animateWithDuration(0.3, animations: {
    self.blurBg.alpha = 1
}, completion: {(_) -> Void in
    self.blurBg.hidden = true
})

Swift 3、4、5

UIView.animate(withDuration: 0.3, animations: {
    self.blurBg.alpha = 1
}, completion: {(_) -> Void in
    self.blurBg.isHidden = true
})

我将 Bool 区域设为下划线的原因是因为您没有使用该值,如果需要,可以将 (_) 替换为 (value : Bool)

【讨论】:

    【解决方案3】:

    animateWithDuration 中的完成参数采用一个带有一个布尔参数的块。在 Swift 中,就像在 Obj-C 块中一样,您必须指定闭包采用的参数:

    UIView.animateWithDuration(0.2, animations: {
        self.blurBg.alpha = 1
    }, completion: {
        (value: Bool) in
        self.blurBg.hidden = true
    })
    

    这里的重要部分是(value: Bool) in。这告诉编译器这个闭包接受一个标有“值”的 Bool 并返回 Void。

    作为参考,如果你想写一个返回 Bool 的闭包,语法应该是

    {(value: Bool) -> bool in
        //your stuff
    }
    

    【讨论】:

    • 也可以通过使用$0作为第一个参数来解决,只需使用块内的变量,编译器就会发现你的块接受了一个参数。
    • dat 完成块语法 :(
    【解决方案4】:

    in 关键字旁边的下划线本身将忽略输入

    斯威夫特 2

    UIView.animateWithDuration(0.2, animations: {
        self.blurBg.alpha = 1
    }, completion: { _ in
        self.blurBg.hidden = true
    })
    

    Swift 3、4、5

    UIView.animate(withDuration: 0.2, animations: {
        self.blurBg.alpha = 1
    }, completion: { _ in
        self.blurBg.isHidden = true
    })
    

    【讨论】:

      【解决方案5】:

      上面有我的解决方案,基于上面接受的答案。它会淡出视图并在几乎不可见时将其隐藏。

      斯威夫特 2

      func animateOut(view:UIView) {
      
          UIView.animateWithDuration (0.25, delay: 0.0, options: UIViewAnimationOptions.CurveLinear ,animations: {
              view.layer.opacity = 0.1
              }, completion: { _ in
                  view.hidden = true
          })   
      }
      

      斯威夫特 3、4、5

      func animateOut(view: UIView) {
      
          UIView.animate(withDuration: 0.25, delay: 0.0, options: UIView.AnimationOptions.curveLinear ,animations: {
              view.layer.opacity = 0.1
          }, completion: { _ in
              view.isHidden = true
          })
      }
      

      【讨论】:

        【解决方案6】:

        有时您希望将其放入一个变量中,以便根据情况以不同的方式制作动画。为此,您需要

         let completionBlock : (Bool) -> () = { _ in 
         }
        

        或者你可以使用同样冗长的:

         let completionBlock = { (_:Bool) in 
         }
        

        但无论如何,您必须在某处指出Bool

        【讨论】:

          【解决方案7】:

          SWIFT 3.x + 4.x

          我想进行更新并简化事情。

          下面的例子是在任何view中实现的,它隐藏得很慢,当它完全透明时;从父级view中删除它自己

          ok 变量将始终返回 true 并终止动画。

              alpha = 1
              UIView.animate(withDuration: 0.5, animations: {
                  self.alpha = 0
              }) { (ok) in
                  print("Ended \(ok)")
                  self.removeFromSuperview()
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-07-13
            • 2023-03-31
            • 2016-12-28
            • 1970-01-01
            相关资源
            最近更新 更多