[swift] CoreAnimation 오파시티 애니메이션

Core Animation은 iOS 및 macOS 애플리케이션에서 높은 성능과 부드러운 애니메이션을 만들기 위한 강력한 프레임워크입니다. 오파시티(Opacity) 애니메이션은 뷰의 투명도를 조절하여 부드럽게 화면을 변환시키는 데 사용됩니다. 이번 포스트에서는 Swift를 사용하여 Core Animation을 이용한 오파시티 애니메이션을 구현하는 방법에 대해 알아보겠습니다.

1. 애니메이션 레이어 생성

먼저 애니메이션을 적용할 뷰의 CALayer를 생성합니다.

let animationLayer = CALayer()
animationLayer.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
animationLayer.backgroundColor = UIColor.blue.cgColor
self.view.layer.addSublayer(animationLayer)

2. 애니메이션 설정

다음으로 애니메이션의 시작 및 끝 값, 시간 등을 설정합니다.

let opacityAnimation = CABasicAnimation(keyPath: "opacity")
opacityAnimation.fromValue = 1.0
opacityAnimation.toValue = 0.2
opacityAnimation.duration = 1.0

3. 애니메이션 적용

애니메이션을 레이어에 적용하고 시작합니다.

animationLayer.add(opacityAnimation, forKey: "opacityAnimation")

위의 코드는 하나의 레이어에 대한 간단한 오파시티 애니메이션을 구현한 것입니다. Keyframes를 사용하여 더 정교한 애니메이션을 구현할 수도 있습니다.

Core Animation을 이용하면 더 다양하고 복잡한 애니메이션도 구현할 수 있으며, 주의할 점은 애니메이션 코드가 UIKit의 뷰 계층구조와 별도로 동작한다는 것입니다.

참고 자료:
Apple Developer Documentation - Core Animation
Ray Wenderlich - Core Animation Tutorial: Gradients and Masks