Blocks on Swift (animateWithDuration:animations:completion:)

前端 未结 7 1797
梦如初夏
梦如初夏 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
    }
    
    0 讨论(0)
  • 2020-12-02 12:11

    Here you go, this will compile

    Swift 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
    })
    

    The reason I made the Bool area an underscore is because you not using that value, if you need it you can replace the (_) with (value : Bool)

    0 讨论(0)
  • 2020-12-02 12:13

    The completion is correct, the closure must accept a Bool parameter: (Bool) -> (). Try

    UIView.animate(withDuration: 0.2, animations: {
        self.blurBg.alpha = 1
    }, completion: { finished in
        self.blurBg.hidden = true
    })
    
    0 讨论(0)
  • 2020-12-02 12:15

    Underscore by itself alongside the in keyword will ignore the input

    Swift 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
    })
    
    0 讨论(0)
  • 2020-12-02 12:16

    Sometimes you want to throw this in a variable to animate in different ways depending on the situation. For that you need

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

    Or you could use the equally verbose:

     let completionBlock = { (_:Bool) in 
     }
    

    But in any case, you have have to indicate the Bool somewhere.

    0 讨论(0)
  • 2020-12-02 12:30

    There is my solution above based on accepted answer above. It fades out a view and hiddes it once almost invisible.

    Swift 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
        })   
    }
    

    Swift 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
        })
    }
    
    0 讨论(0)
提交回复
热议问题