swiftios13uiviewpropertyanimator

Repeat/Autoreverse animations in iOS 13.x


Previously in swift you could do this:

let animator = UIViewPropertyAnimator(duration: 0.25, curve: .easeIn) {
  UIView.setAnimationRepeatCount(Float.infinity)
  UIView.setAnimationRepeatAutoreverses(true)
  let transform = CATransform3DIdentity
  let rotate = CATransform3DRotate(transform, 45, 1, 1, 0)
  self.ex.layer.transform = rotate
}

However, now there is a deprecation message on UIView.setAnimationRepeatCount and UIView.setAnimationRepeatAutoreverses. Does anybody know what they was replaced with? Am I still able to use UIViewPropertyAnimator, or do I have to go to something like CABasicAnimation?

Messages are:

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

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


Solution

  • You can do something like this:

    UIView.animate(withDuration: 0.25, delay: 0, options: [.autoreverse, .curveEaseIn, .repeat], animations: {
        let transform = CATransform3DIdentity
        let rotate = CATransform3DRotate(transform, 45, 1, 1, 0)
        self.ex.layer.transform = rotate
    }, completion: nil)
    

    For all the possible calls, you can check this link

    In addition, if you really needs the UIViewPropertyAnimator, it has a similar init:

     UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 0.25, delay: 0, options: [.autoreverse, .curveEaseIn, .repeat], animations: {
        let transform = CATransform3DIdentity
        let rotate = CATransform3DRotate(transform, 45, 1, 1, 0)
        self.ex.layer.transform = rotate
    })