我旋转并移动一个物体,但在它完成动画后,它会返回到它的初始位置。我在这里查看了有关此内容的其他帖子,例如:CABasicAnimation resets to initial value after animation completes 但是给出的答案没有用。最受欢迎的答案是:
cabasicanimation.fillMode = kCAFillModeForwards;
cabasicanimation.removedOnCompletion = NO;
没有做任何事情。 这是对象的初始化方式:
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
}
}
设置是动画组的一部分,但我只显示移动功能。
这是它在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)
}
}
无论如何都要解决这个问题?