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?