2017-10-08 5 views
2

私はアニメーションを勉強しており、CAKeyframeAnimationを使いたいです。 は、Appleデベロッパに私は次のコードスニペットが見つかりました:CAKeyframeAnimationの使用方法は?

let colorKeyframeAnimation = CAKeyframeAnimation(keyPath: "backgroundColor") 

colorKeyframeAnimation.values = [ 
    UIColor.red.cgColor, 
    UIColor.green.cgColor, 
    UIColor.blue.cgColor 
] 
colorKeyframeAnimation.keyTimes = [0, 0.5, 1] 
colorKeyframeAnimation.duration = 2 

をしかし、私はビューに、このアニメーションオブジェクトを追加する方法についての解決策を見つけていません。それを動作させて何かに適用するには?

答えて

2

ビューのlayerに追加すると、add(_:forKey:)となります。あなたはCoreAnimationでアニメーション化したくない場合は、あなたがUIView.animateKeyframes APIを使用することができます

また

、例えば:

// if the animatedView isn't already .red, set it as such 
// 
// animatedView.backgroundColor = .red 

// then start your animation from the current color, to green, to blue 

UIView.animateKeyframes(withDuration: 2, delay: 0, options: [], animations: { 
    UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5, animations: { 
     self.animatedView.backgroundColor = .green 
    }) 
    UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5, animations: { 
     self.animatedView.backgroundColor = .blue 
    }) 
}, completion: nil) 

注、「相対的」開始時間と期間の値はパーセントです全体のアニメーション期間の

+2

CoreGraphicsではなくCoreAnimationでアニメーション化されると思います。 –

関連する問題