Blocks on Swift (animateWithDuration:animations:completion:)

前端 未结 7 1825
梦如初夏
梦如初夏 2020-12-02 12:08

I\'m having trouble making the blocks work on Swift. Here\'s an example that worked (without completion block):

UIView.animateWithDuration(0.07) {
    self.so         


        
7条回答
  •  旧巷少年郎
    2020-12-02 12:09

    the completion parameter in animateWithDuration takes a block which takes one boolean parameter. In swift, like in Obj C blocks, you must specify the parameters that a closure takes:

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

    The important part here is the (value: Bool) in. That tells the compiler that this closure takes a Bool labeled 'value' and returns void.

    For reference, if you wanted to write a closure that returned a bool the syntax would be

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

提交回复
热议问题