2017-10-08 6 views
2

애니메이션을 공부하고 있는데 CAKeyframeAnimation을 사용하고 싶습니다. 애플 개발자에 나는 다음과 같은 코드 발견 :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

보기의 layeradd(_:forKey:)으로 추가합니다. 은 "상대"시작 시간과 지속 시간 값을 백분율이다

// 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) 

참고 : 당신은 코어 애니메이션과 애니메이션을 원하지 않는 경우


또는, 당신은 UIView.animateKeyframes API, 예를 사용할 수 있습니다 전체 애니메이션 지속 시간의

+2

저는 CoreGraphics보다는 CoreAnimation으로 움직일 것이라고 말하고 싶습니다. –