4

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.

linus_hologram
  • 1,595
  • 13
  • 38

1 Answers1

2

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks for your answer. Would you mind providing example code? I am not experienced in Animations and got the code mentioned from Stack Overflow – linus_hologram Oct 25 '19 at 15:58
  • Sure, what's your animation curve? Is it built-in or custom? – matt Oct 25 '19 at 15:59
  • Thanks! I don’t know the difference to be honest? Do you need more code? – linus_hologram Oct 25 '19 at 16:00
  • 1
    Mmmm, maybe, but I don't think so; I think you're fetching the animation curve from the keyboard in order to move something together with the keyboard, right? If you are doing that in response to a `keyboardWillShow` notification, you don't need to animate! You're already in an implicit animation; just say what you want your constant to be and it will be animated, automatically. – matt Oct 25 '19 at 16:03
  • That’s correct! I remember it being about having the same animation speed as the keyboard. – linus_hologram Oct 25 '19 at 16:05
  • I added some more code, I hope it’s useful for you. I would be very grateful for example code :) thanks in advance – linus_hologram Oct 25 '19 at 16:09
  • Well, your code is very weird, but as I say, you can just delete your animation code (and your `layoutIfNeeded`). Just set the constraint `constant` and you're all set. – matt Oct 25 '19 at 16:11
  • So I can completely remove the UIView.animate stuff and the layoutIfNeeded and only change the constraints? – linus_hologram Oct 25 '19 at 16:17
  • I think so, yes. However, please note that we are now outside the realm of the question you asked. You asked what replaces a certain deprecated method, and I told you the answer to that. If you're having trouble with responding to the keyboard appearing, that's a different question. :) – matt Oct 25 '19 at 16:22
  • Hey matt :) Thanks for your help! I was now able to manage getting it working using UIViewPropertyAnimator. One question: So, UIViewPropertyAnimator would be used if you want the animation to be interactive e.g. a user is dragging on the screen and depending on the finger position the animation moves etc. right? – linus_hologram Oct 27 '19 at 10:26
  • Since which iOS versions is it an implicit animation @matt? – Rivera Apr 17 '20 at 19:39
  • 1
    @Rivera Good question! It wasn't and then it was. :) I don't know when the change happened. – matt Apr 17 '20 at 19:42