Blocks on Swift (animateWithDuration:animations:completion:)



我在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的答案。

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类型,标记为value,并返回Void。

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

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

补全是正确的,闭包必须接受Bool参数:(Bool) -> ()。试着

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

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

根据上面公认的答案,我给出了上面的解决方案。它会淡出一个视图,并隐藏它曾经几乎不可见。

迅速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
    })
}

好了,这将编译

迅速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)

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

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

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

 let completionBlock = { (_:Bool) in 
 }

但是无论如何,你必须在某处标明Bool

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

最新更新