Core Animation ?
- Core Animation is a graphics rendering and animation infrastructure available on both iOS and OS X that you use to animate the views and other visual elements of your app
- infrastructure => ๊ฐ๋ฐ์๋ ์์ ๋ฐ ์ข ๋ฃ์ง์ ๊ณผ ๊ฐ์ ๋ช๊ฐ์ง ์ ๋๋ฉ์ด์ ๋งค๊ฐ๋ณ์๋ฅผ ๊ตฌ์ฑํ๊ณ Core Animation์ด ์์ํ๋๋ก ์ง์ํ๋ฉด, Core Animation์ ์ง์ ์ ๋๋ฉ์ด์ ์ ์กฐ์ํ๋๊ฒ ์๋๋ผ ์ค์ ๋๋ก์ ์์ ์ ์จ๋ณด๋ ๊ทธ๋ํฝ ํ๋์จ์ด๋ก ์ ๋ฌํจ์ผ๋ก์จ CPU์ ๋ถ๋ด์ ์ฃผ์ง์๊ณ ์ ๋๋ฉ์ด์ ์ ๋ง๋ ๋ค
- Core Animation ์ธํ๋ผ์ ํต์ฌ์๋ Layer๊ฐ์ฒด(CALayer)๊ฐ ์๋ค
CALayer ?
- An object that manages image-based content and allows you to perform animations on that content.
- Layer๋ ์์ฒด ๋ชจ์(own appearance)์ ์ ์ํ์ง ์๊ณ . ๋นํธ๋งต์ ๋๋ฌ์ผ ์ํ์ ๋ณด๋ง ๊ด๋ฆฌ => onscreen์ ๋ํ๋ด๊ธฐ ์ํด ์ฌ์ฉ๋๋ visual conten์ content์ ์์ฑ(such as a background color, border, and shadow), content์ geometry (such as its position, size, and transform)๋ฑ์ ๊ด๋ฆฌ
- ๋๋ถ๋ถ์ Layer๊ฐ์ฒด๋ ์ฑ์์ ์ค์ drawing์ ์ํํ์ง ์๋๋ค. ๋์ , Layer๋ ์ฑ์์ ์ ๊ณตํ๋ ์ปจํ ์ธ ๋ฅผ ์บก์ณํ์ฌ backing store ๋ผ๊ณ ๋ถ๋ฆฌ๋ ๋นํธ๋งต์ผ๋ก ์บ์ํด๋์์ผ๋ก์จ ๊ทธ๋ํฝ ํ๋์จ์ด๋ก ์ฝ๊ฒ ์กฐ์ ํ ์ ์๋๋กํ๋ค.
- ๋นํธ๋งต ์์ฒด๋ view drawing ์์ฒด ๋๋ fixed image์ผ ์ ์๊ธฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ๋ Layer๋ ๋ชจ๋ธ ๊ฐ์ฒด๋ก ๊ฐ์ฃผ
- ์ดํ์ Layer์ ํ๋กํผํฐ๋ฅผ ๋ณ๊ฒฝํ๋ฉด, Layer๊ฐ์ฒด์ ๊ด๋ จ๋ ์ํ ์ ๋ณด(state information)๊ฐ ๋ณ๊ฒฝ๋๊ณ , ๋ณ๊ฒฝ์ฌํญ์ด ์ ๋๋ฉ์ด์ ์ ํธ๋ฆฌ๊ฑฐํ๋ฉด Core Animation์ Layer์ ๋นํธ๋งต ๋ฐ ์ํ์ ๋ณด๋ฅผ ๊ทธ๋ํฝ ํ๋์จ์ด๋ก ์ ๋ฌํ์ฌ ํ๋์จ์ด๊ฐ ์ ๋๋ฉ์ด์ ์ ์ํ
CABasicAnimation
- An object that provides basic, single-keyframe animation capabilities for a layer property
- ์ฆ ๋ ์ด์ด ์์ฑ์ ๋ํด ์ ๋๋ฉ์ด์ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ๊ฐ์ฒด
- CABasicAnimation ์ธ์คํด์ค๋ ์ ๋๋ฉ์ด์ ๋ฐฉ์์ ํน์ ํ๋ key path๋ฅผ ์ค์ ํ์ฌ ์์ฑ
์์ ์ฝ๋
import UIKit
class ViewController: UIViewController {
private struct KeyPath {
struct Position {
static let ps = "position"
static let x = "position.x"
}
}
private var square: CALayer = CALayer()
let image = UIImage(named: "green.jpg")
override func viewDidLoad() {
super.viewDidLoad()
configureSquare()
configureButton()
}
func configureSquare() {
square.frame = CGRect(x: 50, y: 100, width: 50, height: 50)
square.contents = image?.cgImage
view.layer.addSublayer(square)
}
func configureButton() {
let animationButton = UIButton(frame: CGRect(x: 300, y: 100, width: 50, height: 50))
animationButton.addTarget(self, action: #selector(moveAction), for: .touchUpInside)
animationButton.setTitle("Move", for: .normal)
animationButton.setTitleColor(.green, for: .normal)
view.addSubview(animationButton)
}
@objc func moveAction() {
let animation = CABasicAnimation()
animation.keyPath = KeyPath.Position.x
animation.fromValue = 50
animation.toValue = 150
animation.duration = 3
animation.fillMode = CAMediaTimingFillMode.forwards
animation.isRemovedOnCompletion = false
self.square.add(animation, forKey: "basic")
self.square.position = CGPoint(x: 150, y: 100)
}
}
์ฐธ๊ณ ์๋ฃ
https://developer.apple.com/documentation/quartzcore
https://zeddios.tistory.com/747
https://sihyungyou.github.io/iOS-CoreAnimation/
https://woozzang.tistory.com/141?category=909641
https://green1229.tistory.com/76
https://jeonyeohun.tistory.com/363?category=881841
'iOS ๐ > iOS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
MVVM-C (0) | 2022.03.28 |
---|---|
Xcode 13, No StoryBoard Settings without SceneDelegate (0) | 2022.03.22 |
CALayer vs UIView (0) | 2022.03.18 |
Animation 1 - UIView Animation (0) | 2022.03.17 |
Core Graphics๋ก ๋ ์ด๋ฏธ์ง ํฉ์ฑํ๊ธฐ (0) | 2022.03.17 |