xcode UIView.animateWithDuration 完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25595456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:27:54  来源:igfitidea点击:

UIView.animateWithDuration completion

iosswiftxcodeuiviewanimation

提问by c2programming

I have a question concerning the swift implementation of the method mentioned in the title. If I do this:

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

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

I get the following problem: Missing argument for parameter 'delay' in call. This only happens if I have the self.navigationController.returnToRootViewController() in the completion part. If I extract that statement into a seperate method like this:

我遇到以下问题:调用中缺少参数“延迟”的参数。只有当我在完成部分有 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)
}

Then it works perfectly and does exactly what I want. Of course this does not seem to be the ideal solution and more like a work around. Can anyone tell me what I did wrong or why Xcode (beta 6) is behaving this way?

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

回答by rickster

I presume you mean popToRootViewControllerAnimatedin your first snippet, since returnToRootViewControllerisn't a method on UUNavigationController.

我想你的意思是popToRootViewControllerAnimated在你的第一个片段中,因为returnToRootViewController它不是UUNavigationController.

Your problem is that popToRootViewControllerAnimatedhas a return value (the array of view controllers removed from the navigation stack). This causes trouble even though you're trying to discard the return value.

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

When Swift sees a function/method call with a return value as the last line of a closure, it assumes you're using the closure shorthand syntax for implicit return values. (The kind that lets you write things like someStrings.map({ $0.uppercaseString }).) Then, because you have a closure that returns something in a place where you're expected to pass a closure that returns void, the method call fails to type-check. Type checking errors tend to produce bad diagnostic messages — I'm sure it'd help if you filed a bugwith the code you have and the error message it's producing.

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

Anyhow, you can work around this by making the last line of the closure not be an expression with a value. I favor an explicit return:

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

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

You can also assign that popToRootViewControllerAnimatedcall to an unused variable or put an expression that does nothing after it, but I think the returnstatement is clearest.

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