iOS 13中代替'setAnimationCurve'的方法

4

iOS 13中,苹果公司已经放弃了我在应用程序中使用的许多功能。 对于其中的大部分功能,已经在StackOverflow上有很好的替代方法解释 - 但对于“setAnimationCurve”没有。

“setAnimationCurve”在iOS 13.0中已被弃用:改用基于块的动画API

这是我具体的代码:

  // MARK: - Keyboard up/down adjustment for the addMediaBar

@objc func keyboardWillShow(notification: NSNotification) {

    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {

        let userInfo = notification.userInfo! as [AnyHashable: Any]

        let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber

        let animationCurve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber



        if addMediaBarBottomAnchor.constant == 0 {

            let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first



            if let bottomPadding = window?.safeAreaInsets.bottom {

                print(keyboardSize.height)

                print(bottomPadding)



                UIView.setAnimationCurve(UIView.AnimationCurve(rawValue: animationCurve.intValue)!)

                UIView.animate(withDuration: animationDuration.doubleValue) {

                    self.addMediaBarBottomAnchor.constant = -keyboardSize.height + bottomPadding

                    self.view.layoutIfNeeded()

                }



            } else {

                UIView.setAnimationCurve(UIView.AnimationCurve(rawValue: animationCurve.intValue)!)

                UIView.animate(withDuration: animationDuration.doubleValue) {

                    self.addMediaBarBottomAnchor.constant = -keyboardSize.height

                    self.view.layoutIfNeeded()

                }

            }



        }

    }

}

每当键盘出现时,我使用这段代码来上下滑动屏幕底部的一栏。

对于这个主题的任何帮助,我将非常感激。

1个回答

2

如果您想要内置的动画曲线,请调用

animate(withDuration:delay:options:animations:completion:)

options 参数允许您包含动画曲线。

但更好的选择是完全不使用 UIView 类动画调用。使用 UIViewPropertyAnimator。现在您可以应用任何动画曲线。


1
嗯,也许吧,但我不这么认为;我想你是从键盘中获取动画曲线,以便与键盘一起移动某些东西,对吗?如果您是在响应keyboardWillShow通知时执行此操作,则无需进行动画处理!您已经处于隐式动画状态;只需说明您希望常量是什么,它就会自动进行动画处理。 - matt
没错!我记得它是关于动画速度与键盘相同的。 - linus_hologram
嗯,你的代码非常奇怪,但正如我所说,你可以删除你的动画代码(以及你的layoutIfNeeded)。只需设置约束constant,你就完成了。 - matt
嘿,马特 :) 谢谢你的帮助!现在我已经能够使用 UIViewPropertyAnimator 来管理它的工作了。一个问题:那么,如果您想要动画是交互式的,例如用户正在屏幕上拖动,并且根据手指位置移动动画等,那么应该使用 UIViewPropertyAnimator,对吗? - linus_hologram
1
@Rivera 很好的问题!一开始不是,后来变成了。 :) 我不知道这个变化是什么时候发生的。 - matt
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接