2017-08-29 6 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

あなたの問題は、あなたがあなたの代わりにアニメーションキーパスとして「position.x」のtransform.translation.xを使用する必要testView.transform = .identityを使用してアニメーションをリセットしたい場合は、唯一の呼び出しも必要とされていないコール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()'アニメーションを削除する呼び出しできないのだろうか? –

関連する問題