簡體   English   中英

UIView.animateWithDuration完成

[英]UIView.animateWithDuration completion

關於標題中提到的方法的快速實現,我有一個問題。 如果我這樣做:

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

我遇到以下問題:在調用中缺少參數'delay'的參數。 只有在完成部分中有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有一個返回值(從導航堆棧中刪除的視圖控制器數組)。 即使您嘗試丟棄返回值,這也會導致麻煩。

當Swift看到一個帶有返回值的函數/方法調用作為閉包的最后一行時,它假定您正在使用閉包簡寫語法來獲取隱式返回值。 (允許你編寫someStrings.map({ $0.uppercaseString }) 。)然后,因為你有一個閉包,它返回一個你希望傳遞一個返回void的閉包的地方,方法調用沒有打字檢查。 類型檢查錯誤往往會產生錯誤的診斷消息 - 我確信如果你提交了一個包含你所擁有的代碼的錯誤以及它產生的錯誤消息,它會有所幫助。

無論如何,你可以通過使閉包的最后一行不是帶有值的表達式來解決這個問題。 我贊成明確的return

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

你也可以將popToRootViewControllerAnimated調用分配給一個未使用的變量,或者放一個在它之后什么都不做的表達式,但我認為return語句是最清晰的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM