ios - removeAllAnimations 不起作用后添加动画

标签 ios core-animation caanimation

我有一个简单的应用程序,它是 red View 变换位置从左屏幕到右屏幕。我希望当我点击 remove 按钮时,动画将重新启动(red View 变换位置再次从左屏幕到右屏幕)但它不起作用。 red View 回到原始帧,但它没有移动,但 animationDidStop 仍然在持续时间后调用。

class TestViewController: UIViewController, CAAnimationDelegate {

let testView : UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .red
    view.layer.borderColor = UIColor.black.cgColor
    view.layer.cornerRadius = 5
    return view
}()

let removeAniButton : UIButton = {
    let btn = UIButton(type: .system)
    btn.translatesAutoresizingMaskIntoConstraints = false
    btn.setTitle("Remove", for: .normal)
    return btn
}()

let addAniButton : UIButton = {
    let btn = UIButton(type: .system)
    btn.translatesAutoresizingMaskIntoConstraints = false
    btn.setTitle("Add", for: .normal)
    return btn
}()

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = .white

    view.addSubview(testView)
    testView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
    testView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
    testView.heightAnchor.constraint(equalToConstant: 100).isActive = true
    testView.widthAnchor.constraint(equalToConstant: 100).isActive = true

    view.addSubview(removeAniButton)
    removeAniButton.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50).isActive = true
    removeAniButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    removeAniButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
    removeAniButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
    removeAniButton.addTarget(self, action: #selector(removeAnimation), for: .touchUpInside)

    view.addSubview(addAniButton)
    addAniButton.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 150).isActive = true
    addAniButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    addAniButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
    addAniButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
    addAniButton.addTarget(self, action: #selector(addAnimation), for: .touchUpInside)

    addAnimation()
    // Do any additional setup after loading the view.
}

func createAnimation() -> CAAnimation {
    let animation = CABasicAnimation(keyPath: "position.x")
    animation.fromValue = 0
    animation.toValue = self.view.frame.width
    animation.duration = 4
    animation.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionLinear) 
    animation.fillMode = kCAFillModeForwards
    animation.isRemovedOnCompletion = false
    animation.delegate = self
    return animation
}

func removeAnimation(){
    testView.layer.removeAllAnimations()
    testView.transform = .identity
    addAnimation()
}

func addAnimation(){
    testView.layer.add(createAnimation(), forKey: nil)
}

func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
    if flag {
        print("Animation is finished")
    }
  }
}

谁能解释一下?

最佳答案

您的问题是您为动画使用了错误的 keyPath,如果您想使用 testView.transform = .identity 重置动画,您需要使用 transform.translation.x 而不是“position.x”作为动画 keyPath,也不需要调用 removeAllAnimations() 只需再次调用您的 addAnimation 即可完成工作

使用此代码创建动画

func createAnimation() -> CAAnimation {
    let animation = CABasicAnimation(keyPath: "transform.translation.x")
    animation.fromValue = 0
    animation.toValue = self.view.frame.width
    animation.duration = 4
    animation.timingFunction = CAMediaTimingFunction.init(name: kCAMediaTimingFunctionLinear)
    animation.fillMode = kCAFillModeForwards
    animation.isRemovedOnCompletion = false
    return animation
}

func removeAnimation(){
    //customView.layer.removeAllAnimations()
    //customView.transform = .identity
    addAnimation()
}

关于ios - removeAllAnimations 不起作用后添加动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45939131/

相关文章:

ios - 为什么每次我尝试显示两个 GLKView Controller 的 subview Controller 时,我的应用程序都会崩溃?

objective-c - 如何在切换 subview 的同时围绕 x 轴翻转 UIView

iphone - 如何删除动画(CABasicAnimation)?

swift - 彩色动画

ios - 如果为其设置了 beginTime,CAEmitterLayer 不会添加基本动画

ios - CAAnimationGroup 中的 CAAnimation 委托(delegate)

iphone - OpenGL 不显示 iPhone 上辅助线程的结果

ios - 如何在编译时检查当前项目中是否存在 Swift 模块?

ios - 通过 ARM NEON 进行向量矩阵乘法

ios - 为什么我的雪没有落到地上?