Search code examples
iosswiftuikitios13ios-animations

iOS 13 Alternative to 'setAnimationCurve'


with iOS 13 Apple deprecated a lot of functions that I've been using in my app. For most of them, there are already alternatives well explained on StackOverflow - however not for 'setAnimationCurve'.

'setAnimationCurve' was deprecated in iOS 13.0: Use the block-based animation API instead

This is the exact code I have:

  // 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()

                }

            }



        }

    }

}

I am using this code to slide up/down a bar on the bottom of the screen, whenever the keyboard shows up.

I would be really grateful for any help on this topic.


Solution

  • If you want a built-in animation curve, call

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

    The options: lets you include an animation curve.

    But an even better choice is not to use UIView class animation calls at all. Use a UIViewPropertyAnimator. Now you can apply any animation curve you like.