iOS Swift 3 如何在动画完成后执行动作

Joc*_*her 1 scroll uikit ios swift swift3

我使用以下代码来确保当用户打开键盘时,它会缓慢打开并将视图向上推。

func keyboardWasShown(notification: NSNotification){
    tableViewChat?.tableView.isScrollEnabled = true
    let info2 = notification.userInfo!
    let keyboardFrame: CGRect = (info2[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue

    UIView.animate(withDuration: 4, animations: { () -> Void in
        self.bottomConstraint.constant = keyboardFrame.size.height - self.navigationController!.navigationBar.frame.height
    })
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切都像魅力一样发挥作用。现在我想在动画完成后执行这一行。

self.tableViewChat?.scrollToLastRow(animationBool: true)
Run Code Online (Sandbox Code Playgroud)

如果动画未完全执行,则表视图将无法向下滚动足够的距离。animate执行后如何执行scrollToLastRow?

感谢您的帮助和评论!

The*_*oup 5

只需使用动画的完成块即可。

UIView.animate(withDuration: 4, animations: { () -> Void in
    self.bottomConstraint.constant = keyboardFrame.size.height - self.navigationController!.navigationBar.frame.height
 },completion{
    self.tableViewChat?.scrollToLastRow(animationBool: true)
})
Run Code Online (Sandbox Code Playgroud)

在动画完成之前,这不会滚动桌面视图。在本例中为 4 秒。