2016-09-19 7 views
2
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キーワードを必要とし、言ったように私は間違っ[弱い自己]が次のコードでエラーを起こしています:期待された '、'セパレータ

+2

'[弱い自己] in'、あなたは' in'を持っていません – Tj3n

答えて

1

何をやっていますアニメーションブロックは、アニメーションの期間selfに強い参照を保持していないため、

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]を必要としません。中断する強い参照サイクルはありません。だから、私は[weak self]を完全に削除することを提案したい。

そして、ビューが却下されたときに、進行中のアニメーションがキャンセルされ、completionブロックはすぐにfalseと呼ばれているので、あなたが迷っている場合には、あなたは、どちらか、completionブロックで強い参照を心配する必要はありません。 booleanパラメータの場合

+0

ありがとう。私は後で、アニメーションのブロックに[弱い自己]は必要ないと考えました。 –

-3
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) 

     } 

    } 
} 
関連する問題