我很难让Swift 的积木正常工作.下面是一个成功的示例(没有完成挡路):

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

或者,也可以不使用尾部闭合:

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

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

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

自动完成功能给了我completion: ((Bool) -> Void)?分,但我不确定如何让它工作.我还try 了尾部闭合,但得到了相同的错误:

! 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.

推荐答案

animateWithDuration中的完成参数采用一个块,该块采用一个布尔参数.在Swift中,与Obj-C块中一样,必须指定闭包采用的参数:

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

这里最重要的部分是(value: Bool) in.这告诉编译器,这个闭包接受一个标记为"value"的Bool并返回Void.

作为参考,如果您想编写一个返回Bool的闭包,语法如下

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

Ios相关问答推荐

将图案UI视图动画化以模拟进度条

什么是最好的方式使用DateFormatters在可扩展视图cellForRowAt方法

SWIFT设备方向更新不正确

如何在iPhone 15上消除.NET Maui中状态栏和页面之间的差距

不使用iOS 17修改器修改SWIFT用户界面视图

Swift导航远离视频导致崩溃

多协议和类继承混乱,Swift

UIView 阴影不适用于自定义扩展

RealityKit – ARView 光线投射返回 0 个结果

如何在 SwiftUI 中对表格行使用 Transferable

判断Apple LiveText是否可用?

如何将 ImageView 放置在 UIView 的中心?

如何在 Swift 上的 sendSynchronousRequest 中判断 Response.statusCode

在 UICollectionView 上从右到左对齐

有什么方法可以预填充核心数据?

不变违规:应用程序 AwesomeProject 尚未注册使用静态 jsbundle 为 iOS 设备构建时

使用 AVAudioPlayer 播放声音

检测 iOS UIDevice 方向

关闭通过模态 segue 显示的视图

使用 swift 从应用程序委托打开视图控制器