[英]The [weak self] is giving an error for the following code: Expected ',' Separator
fileprivate func hideViewWithAnimation() {
UIView.animate(withDuration: 0.3, animations: { [weak self]
if self == nil {
return
}
self!.view.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
self!.constraintContainerViewBottom.constant = -Constants.screenHeight()
self!.constraintContainerViewTop.constant = Constants.screenHeight()
self!.view.layoutIfNeeded()
}, completion: { (isCompleted) in
self.navigationController?.dismiss(animated: false, completion: nil)
})
}
對於[弱自我]要求使用','將其分開時顯示錯誤。 我究竟做錯了什么
正如Tj3n所說,當您使用[weak self]
語法時,您需要in
關鍵字,例如
UIView.animate(withDuration: 0.3) { [weak self] in
self?.view.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
...
}
但是,您根本不需要在動畫塊中使用[weak self]
,因為在動畫期間,動畫塊不會對self
進行強烈引用。 沒有強大的參考周期可以打破。 因此,我建議完全刪除[weak self]
。
而且,如果您想知道,您也不必擔心completion
塊中的強引用,因為當關閉視圖時,動畫會被取消,並且會立即為布爾參數使用false
來調用completion
塊。 。
private func hideViewWithAnimation() {
weak var weakSelf = self
if weakSelf != nil {
UIView.animateWithDuration(0.3, animations: {
self!.view.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
self!.constraintContainerViewBottom.constant = -Constants.screenHeight()
self!.constraintContainerViewTop.constant = Constants.screenHeight()
self!.view.layoutIfNeeded()
}) { (isCompleted) in
self.navigationController?.dismiss(animated: false, completion: nil)
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.