0

I'm rotating and moving an object but after it's finished animating it returns to its initial position. I've looked at other posts about this on here like: CABasicAnimation resets to initial value after animation completes but the answers given didn't work. The most popular answer:

cabasicanimation.fillMode = kCAFillModeForwards;
cabasicanimation.removedOnCompletion = NO;

didn't do anything. This is how the object is initialized:

class coreAnimatedNewView: CALayer {


  var animationGroup = CAAnimationGroup()
  var animationDuration: TimeInterval = 1.5


  override init(layer: Any) {
    super.init(layer: layer)


  }

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
  }


  init(frame: CGRect) {
    super.init()

    self.frame = frame


      DispatchQueue.main.async {
       self.add(self.createTranslationAnimation(), forKey: "shape")

    }
  }


  //Move Animation
  func createTranslationAnimation () -> CABasicAnimation {
    let moveAnimation = CABasicAnimation(keyPath: "transform.translation")
    moveAnimation.fromValue = NSValue(cgPoint:CGPoint(x: 10, y: 10))
    moveAnimation.toValue = NSValue(cgPoint:CGPoint(x: 120, y: 20))
    moveAnimation.duration = animationDuration

    return moveAnimation

 }
}

The setup is part of an animation group but I'm only showing the move function.

This is how it's setup in the viewcontroller:

class CoreAnimateViewController: UIViewController {


  //Remember to switch initial viewController on storyboard!!!
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)

    addShape()

  }

  func addShape () {

    let shape = coreAnimatedNewView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
    self.view.layer.addSublayer(shape)


  }

}

Is there anyway to solve this issue?

SwiftyJD
  • 5,257
  • 7
  • 41
  • 92
  • What is the object? How is it positioned initially? – Paulw11 May 08 '17 at 00:56
  • I've updated the question with the initialization – SwiftyJD May 08 '17 at 01:00
  • Sorry, but can you clarify how the instance of `coreAnimatedNewView` is positioned initially; What I am trying to understand is if it is positioned by auto layout; if so then modifying it's frame directly won't work; you need to modify the constraints – Paulw11 May 08 '17 at 01:03
  • I updated the code, autolayout is not being used, its all done programatically – SwiftyJD May 08 '17 at 01:05
  • I tried to make a new Swift project and replicate this behavior but there's an error on the line `self.setupAnimationGroup()` within the `coreAnimatedNewView`'s `init` function. Where is that being defined / what version of Swift are you using? – Pat Needham May 08 '17 at 01:15
  • Sorry about that, its part of a larger animation group, I've updated the code to only use that one function, its in Swift 3 – SwiftyJD May 08 '17 at 01:20

0 Answers0