Animation ๊ตฌํ
iOS์์ ๋ณดํต Animation์ ๊ตฌํํ ๋ ๋ฐฉ๋ฒ ํฌ๊ฒ ๋๊ฐ์ง ๋ฐฉ๋ฒ์ด ์กด์ฌํ๋ค.
1. UIView์ Animaition ๊ด๋ จ api
2. Core Animation์ ์ด์ฉํ api
์ด๋ฒ ๋ธ๋ก๊ทธ์์๋ UIView์ Animaition ๊ด๋ จ api๋ค์ ์์๋ณด๋ ค๊ณ ํ๋ค
UIView Animation Api
UIView Animation Api๋ฅผ ์ด์ฉํ์ฌ ์ ๋๋ฉ์ด์ ์ ๋ง๋ค๋์ OS ๋ฒ์ ๋ณ๋ก ๋๊ฐ์ง ๋ฐฉ๋ฒ์ด ์กด์ฌํ๋ค
1. UIView.animate() ๋ฉ์๋
2. UIViewPropertyAnimator ํด๋์ค
UIView.animate()
์ ๋๋ฉ์ด์ ์ด ๊ฐ๋ฅํ ํ๋กํผํฐ : frame, bounds, center, transform, alpha, backgroundColor
์ ๋๋ฉ์ด์ ์ด ์คํ๋๋ ๋์์๋ ์ ์ ์ ์ธํฐ๋ ์ ์ด ๋งํ๋ค
์ง์์๊ฐ, ์ ๋๋ฉ์ด์ ์ ์ต์ (ํ์ด๋ฐ ํจ์ ๋ฑ), ์๋ฃํธ๋ค๋ฌ๋ฑ์ ๋ฉ์๋์ ๊ฐ ์ธ์๋ก ๋ถ๋ฆฌํ๊ณ , ์ ๋๋ฉ์ด์ ์ด ๊ฐ๋ฅํ ํ๋กํผํฐ๋ค์ ๋ณ๊ฒฝ์ฌํญ์ ํด๋ก์ ์์ ์์ฑ (๊ทธ๋ฆผ1)
๋ฐ๋ฉด Auto Layout์ Constraint ์์ฑ์ Animatableํ built-in animation ์์ฑ์ด ์๋๊ธฐ ๋๋ฌธ์ layoutIfNeeded๋ผ๋ ๋ฉ์๋์ ํธ์ถ๋ก ๊ทธ ์ฆ์ ๊ฐ์ ๋ฐ์ํ๋ ์์ ์ ํด์ฃผ๋ ์๋์ผ๋ก ๋ฐ๋์ ํด์ฃผ์ด์ผ ํ๋ค. layoutIfNeeded ๋ฉ์๋๋ก frame์ด ๋๊ธฐ์ ์ผ๋ก ์ฆ์ ๋ณ๊ฒฝ๋จ์๋ฐ๋ผ ์ ๋๋ฉ์ด์ ์์คํ ์ frame์ ๋ง์ง๋ง ์ํ๋ฅผ ์์์๊ธฐ ๋๋ฌธ! (๊ทธ๋ฆผ2)
์ ๋๋ฉ์ด์ ์ ๋ทฐ์ ์์์ํ์ ๋ ์ํ๋ฅผ ํ๋ก๊ทธ๋๋จธ๊ฐ ์ด๋ฏธ ์๊ณ ์์ผ๋ฏ๋ก ์ง์์๊ฐ๊ณผ ํ์ด๋ฐ ๊ณก์ ์ ์ด์ฉํ์ฌ ๊ทธ ์ค๊ฐ ์ํ์ ๊ฐ๋ค์ ๊ณ์ฐํ๋ฉด ์ค์ฒฉ๋(nested) ์ ๋๋ฉ์ด์ ๊ตฌ์กฐ๋ฅผ ํตํด ์ค๊ฐ ํ๋ ์๋ค์ ๋ง๋ค์ด ํ๋ฉด์ ํํํ ์ ์๋ค.(๊ทธ๋ฆผ3)
ํ์ง๋ง ์ค์ฒฉ์ด ๋์ด๋ ์๋ก ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง๊ธฐ์ ์ด๋ด๋๋ UIView.animate()๋์ UIView.animatekeyframes() ์ฌ์ฉํ์ฌ ์๊ฐ ์์๋๋ก ์ ๋๋ฉ์ด์ ์ ๋์์ํฌ์ ์๋ค (๊ทธ๋ฆผ4)
UIViewPropertyAnimator
UIView.animate()๋ ์ฌ์ฉ์์ ์ ๋ ฅ๊ณผ ์ํธ์์ฉํ๋ ์ ๋๋ฉ์ด์ ์ ์์ฑํ๋๋ฐ ํ๊ณ๋ฅผ ๊ทน๋ณตํ๊ณ ์ iOS 10๋ถํฐ UIViewPropertyAnimator ๋์ ๋์๋ค
๊ธฐ์กด์ UIView.animate()์ ๊ฐ์ด ์ง์์๊ฐ๊ณผ ํ์ด๋ฐ ๊ณก์ ๊ทธ๋ฆฌ๊ณ ์ ๋๋ฉ์ด์ ์ ๋ด์ฉ์ ๋ด์ ํด๋ก์ ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์์ฑํ๋, ์ ๋๋ฉ์ด์ ์ด ์์๋์์ ๋, ์ด๋ฅผ ์ผ์์ ์ง๋ ์ค๋จํ ์ ์๊ณ , ์ญ๋ฐฉํฅ์ผ๋ก ๋๊ฐ๊ฑฐ๋ ํน์ ์์น๋ก ์ฎ๊ฒจ๊ฐ๋ ๋ฑ์ ๋์์ ์ฒ๋ฆฌํ ์ ์๊ฒ ํด์ค๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก ์ ์์ ์คํ์ ๋ถ๋ฆฌํ ์ ์๋ค(๊ทธ๋ฆผ1)
// ์ ๋๋ฉ์ด์
์ ์ ๋ฐ ์ฆ์์คํ
UIViewPropertyAnimator
.runningPropertyAnimator(withDuration: duration, delay: 0, options: [.curveEaseInOut]) {
self.sampleView2.frame = .init(x: 100, y: 300, width: 150, height: 200)
}
// ๋ฐฉ๋ฒ2: ์ ๋๋ฉ์ด์
์ ์, ์คํ ๋ถ๋ฆฌ
// ์ ์
let animation = UIViewPropertyAnimator(duration: duration, curve: .easeInOut) {
self.sampleView2.frame = .init(x: 100, y: 300, width: 150, height: 200)
}
// ์คํ
animation.startAnimation()
์ด์ธ์ ์ ๊ณต๋๋ api๋ฅผ ํตํด ์ ๋๋ฉ์ด์ ์ด ์๋ฃ๋๊ธฐ ์ ๊น์ง ๋์ ์ผ๋ก ์ ๋๋ฉ์ด์ ์ ์์ ํ ์ ์๋ค.
=> Start, pause, resume, and stop animations; see the methods of the UIViewAnimating protocol.
์ฐธ๊ณ ์๋ฃ
https://developer.apple.com/documentation/uikit/uiviewpropertyanimator
https://developer.apple.com/documentation/uikit/uiview/1622418-animate
https://jinnify.tistory.com/66?category=804637
https://developer.apple.com/documentation/uikit/uiviewpropertyanimator
'iOS ๐ > iOS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Animation 2 - Core Animation (0) | 2022.03.21 |
---|---|
CALayer vs UIView (0) | 2022.03.18 |
Core Graphics๋ก ๋ ์ด๋ฏธ์ง ํฉ์ฑํ๊ธฐ (0) | 2022.03.17 |
Pixel, PT, PPI (0) | 2022.03.16 |
Deep Link : URI Scheme vs Universal Link (0) | 2022.03.16 |