ios - UIView.animateWithDuration 完成

标签 ios swift xcode uiviewanimation

我有一个关于快速实现标题中提到的方法的问题。如果我这样做:

leadingSpaceConstraint.constant = 0
UIView.animateWithDuration(0.3, animations: {
    self.view.layoutIfNeeded()
}, completion: { (complete: Bool) in
    self.navigationController.returnToRootViewController(true)
})

我遇到以下问题:调用中缺少参数“延迟”的参数。只有当我在完成部分有 self.navigationController.returnToRootViewController() 时才会发生这种情况。如果我将该语句提取到像这样的单独方法中:

leadingSpaceConstraint.constant = 0
UIView.animateWithDuration(0.3, animations: {
    self.view.layoutIfNeeded()
}, completion: { (complete: Bool) in
    self.returnToRootViewController()
})

func returnToRootViewController() {
    navigationController.popToRootViewControllerAnimated(true)
}

然后它完美地工作并且完全按照我的要求进行。当然,这似乎不是理想的解决方案,更像是一种变通方法。谁能告诉我我做错了什么或者为什么 Xcode (beta 6) 会这样?

最佳答案

我假设您在第一个代码段中指的是 popToRootViewControllerAnimated,因为 returnToRootViewController 不是 UUNavigationController 上的方法。

您的问题是 popToRootViewControllerAnimated 有一个返回值(从导航堆栈中删除的 View Controller 数组)。即使您试图丢弃返回值,这也会造成麻烦。

当 Swift 看到带有返回值的函数/方法调用作为闭包的最后一行时,它假定您正在使用闭包速记语法来表示隐式返回值。 (那种让你写类似 someStrings.map({ $0.uppercaseString }) 的东西。)然后,因为你有一个闭包,它在你期望传递闭包的地方返回一些东西返回 void,方法调用无法进行类型检查。类型检查错误往往会产生错误的诊断消息——我相信如果你这样做会有所帮助 filed a bug使用您拥有的代码及其产生的错误消息。

无论如何,您可以通过使闭包的最后一行不是具有值的表达式来解决这个问题。我赞成明确的 return:

UIView.animateWithDuration(0.3, animations: {
    self.view.layoutIfNeeded()
}, completion: { (complete: Bool) in
    self.navigationController.popToRootViewControllerAnimated(true)
    return
})

您还可以将 popToRootViewControllerAnimated 调用分配给一个未使用的变量,或者在它后面放置一个不执行任何操作的表达式,但我认为 return 语句最清楚。

关于ios - UIView.animateWithDuration 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25595456/

相关文章:

swift - 在 Swift 3 中组合项目的最酷方式

xcode - 检查对象类型 ios swift

ios - CALayer shouldRasterize 和 UIView drawRect?

ios - 在 SFSafariViewController 中处理弹出窗口/标签

ios - 如何在PDFView中呈现单页pdf文档,使文档居中完整呈现?

ios - Xcode 申请提交失败

iPhone 应用程序图标没有占用 springboard 上图标的全部空间?

ios - Xcode 中未找到架构 i386 - Dropbox SDK 的符号

ios - 球从墙上弹起不起作用

html - 为什么在 iOS 中显示为 PDF 时,具有居中文本的底部表格单元格会被截断?