2017-08-12 93 views
0

我有一個UIViewController,每次單擊時都會觸發它。問題在於它是一種菜單,包含從屏幕底部向上滑動的UIView動畫查看每次查看控制器出現?

現在,最初,底部約束設置爲-500,因此它不在屏幕上。我用一些動畫創建了一個函數,將約束重新設置爲100。問題是我認爲如果我調用viewWillAppear()中的函數,它會每次都會生成動畫。但事實並非如此。我也嘗試在viewDidDisappear()中將約束重置回-500,因此在下次觸發它之前它已關閉屏幕。

不起作用。它只是第一次。我錯過了什麼,或當你改變你的Constraintconstant價值,你就會看到效果,那麼你必須調用layoutIfNeeded()方法我打電話它在錯誤的地方...

class PostVC: UIViewController { 

    @IBOutlet weak var menuBottomConstraint: NSLayoutConstraint! 

    override func viewWillAppear(_ animated: Bool) { 
     super.viewWillAppear(animated) 
     animateMenu() 
    } 

    override func viewWillDisappear(_ animated: Bool) { 
     super.viewWillDisappear(animated) 
     menuBottomConstraint.constant = -500 
    } 

    func animateMenu() { 
     menuBottomConstraint.constant = 100 
     UIView.animate(
      withDuration: 1.0, 
      delay: 0.0, 
      usingSpringWithDamping: 0.5, 
      initialSpringVelocity: 0.0, options: .curveEaseOut, 
      animations: { 
       self.view.layoutIfNeeded() 
      }, 
      completion: nil 
     ) 
    } 
} 
+0

無關,但你不應該硬編碼TRUE;更換viewWillAppear函數調用時'super.viewWill [A DISA]:

在你的代碼

。傳遞'animated'參數。 – rmaddy

回答

1

每次。 | ppear`

override func viewWillDisappear(_ animated: Bool) { 
    super.viewWillDisappear(animated) 
    menuBottomConstraint.constant = -500 
    self.view.layoutIfNeeded() 
} 

我還提出了與viewDidAppear