ios - Swift 中 block 的语法

标签 ios swift swift3

我正在尝试从 Objective-C 重写为 Swift,我无法理解语法或理解文档

这是我在 Objective-C 中编写的一个简化示例:

[UIView animateWithDuration:10.0 animations:^{self.navigationController.toolbar.frame = CGRectMake(0,10,0,10);}];

如何在 Swift 中编写此代码?

这是自动完成的模板:

UIView.animateWithDuration(duration: NSTimeInterval, animations: (() -> Void))

最佳答案

这是 swift 闭包格式:

{(parameter:type, parameter: type, ...) -> returntype in
    //do stuff  
}

这是你应该做的:

//The animation closure will take no parameters and return void (nothing).
UIView.animateWithDuration(duration: NSTimeInterval, animations: {() -> Void in
    //Animate anything.
})

这是 documentation关闭。

关于ios - Swift 中 block 的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24038713/

相关文章:

swift - 如何在 UITableview 标题中添加自定义控件

iOS:何时使用委托(delegate)/数据源(协议(protocol))与属性

ios - Swift 3 核心数据 "Entity"错误 : `Use of undeclared type`

simulator - XCode 8 和 Swift 3 中的 UserDefaults.standard set nil 错误

ios - 无法在 UItableviewcell 中加载 collectionview

iphone - 提高应用程序电池消耗效率的技巧。(iOS)

iphone - Wepopover 控件中的 Uibutton

ios - 如何设计合适的 Firebase/NoSQL 数据结构

IOS 应用程序将图像保存在内存中

ios - 显示单元格边界之外的标签