2017-08-29 89 views
1

我有一个简单的应用程序,即red视图转换位置窗体左屏幕到右屏幕。我想要当我点击remove按钮时,动画将重新启动(red视图变换位置窗体再次从左到右屏幕),但它不起作用。 red查看回原点框架但它不移动,但animationDidStop仍然持续时间后调用。removeAllAnimations之后添加动画不起作用

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") 
    } 
    } 
} 

谁能解释一下吗?

+0

您正在使用的动画一个属性,另一个是回归到他原来的位置,那是你的问题 –

回答

1

你的问题是,你正在使用你的动画错误的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() 
} 
+0

谢谢你的帮助,但我不知道为什么我不能调用'customView.layer.removeAllAnimations()'来删除动画? –