具有keyboardWillShow的平滑动画

问题描述 投票:0回答:1

我正在关注代码。对于我的应用程序,0.03的延迟是一个神奇的数字,我可以看到它的平滑滚动。对于任何其他值,它都不平滑。我想知道是否有确切的公式来计算延迟以使滚动平滑?

@objc func keyboardWillShow(notification: NSNotification) {

    self.topConstraints.constant = 16
    // this lags
    // let duration: TimeInterval = (notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey])as! TimeInterval 

    UIView.animate(withDuration: 0, delay: 0.03, options: UIView.AnimationOptions.curveEaseOut, animations: {
        self.view.layoutIfNeeded()
    }, completion: { (completed) in
    })
}
ios swift uiview
1个回答
0
投票

如果目标是使动画跟随键盘动画,则可以抓取keyboardAnimationDurationUserInfoKeykeyboardAnimationCurveUserInfoKeykeyboardFrameEndUserInfoKey

func keyboardWillShow(with notification: Notification) {
    guard
        let duration = notification.userInfo?[UIApplication.keyboardAnimationDurationUserInfoKey] as? TimeInterval,
        let curve = notification.userInfo?[UIApplication.keyboardAnimationCurveUserInfoKey] as? UInt,
        let frame = notification.userInfo?[UIApplication.keyboardFrameEndUserInfoKey] as? CGRect
    else {
        return
    }

    bottomConstraint.constant = ...
    UIView.animate(withDuration: duration, delay: 0, options: UIView.AnimationOptions(rawValue: curve), animations: {
        self.view.layoutIfNeeded()
    })
}
© www.soinside.com 2019 - 2024. All rights reserved.