์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
- UIKit
- SwiftUI
- ios
- XCTest
- designpattern
- combine
- arkit
- BFS
- Lv2
- node.js
- realm
- Flutter
- ํ๋ก๊ทธ๋๋จธ์ค
- ํจ์คํธ์บ ํผ์ค
- rxcocoa
- reactorkit
- TCA
- raywenderlich
- BOJ
- visionOS
- Kuring
- tableView
- Xcode
- SnapKit
- Swfit
- ๋ฐฑ์ค
- CollectionView
- RxSwift
- swift
- MVVM
- Today
- Total
lgvv98
๐ ch11 ํ์๊ธ ๋ญํน์ฑ ์ฝ๋๋ฆฌ๋ทฐ(Animation)๐ ๋ณธ๋ฌธ
๐ ch11 ํ์๊ธ ๋ญํน์ฑ ์ฝ๋๋ฆฌ๋ทฐ(Animation)๐
๐ฅ ์บ๋ฟ๋งจ 2021. 6. 22. 18:07โ ์ด๋ฒ ์๊ฐ์๋ ch11์์ ์ ๋๋ฉ์ด์ ์ ์ค์ ์ผ๋ก ํ์ฌ ์ฝ๋๋ฆฌ๋ทฐ๋ฅผ ์งํํ ์๊ฐ์ด์ผ.
์ด ํ์ด์ง๋ฅผ ๋ณด๊ธฐ ์ ! ์์ธํ ์ดํด๋ฅผ ์ํด [๊ฐ๋ ํธ]์ ์ฐธ๊ณ ํ๊ธฐ ๋ฐ๋
2021.06.22 - [iOS/ํจ์บ ๊ณต๋ถ] - ๐ ch11 Animation - ๊ฐ๋ ํธ๐
๐ ch11 Animation - ๊ฐ๋ ํธ๐
์ด๋ฒ์๊ฐ์๋ ์ ๋๋ฉ์ด์ ์ ๋ํด์ ๊ฐ๋ตํ๊ฒ ์ ๋ฆฌํด๋ณด๋๋ก ํ์! โ ์ ๋๋ฉ์ด์ ์ด๋? ์๊ฐ์ ๋ฐ๋ผ ๋ทฐ์ ์ํ๊ฐ ๋ฐ๋๋ ๊ฒ ์ ๋๋ฉ์ด์ ์ ์ด์ฉํ๋ฉด ์ฑ์ ๋ ์์ฑ๋ ์๊ฒ ๋ง๋ค ์๋ ์์ง๋ง, ๊ณผํ
rldd.tistory.com
๊ฐ๋ ํธ์์ ์ธ๊ธํ๋ฏ์ด
1. ์ ์ฝ์กฐ๊ฑด์ ํตํด ์ ๋๋ฉ์ด์ ์ ๊ตฌํํ๊ฑฐ๋,
2. ๋ทฐ๋ฅผ ํตํด constraint๋ฅผ ์กฐ์ํ๊ฑฐ๋ ๋๊ฐ์ง ๋ฐฉ๋ฒ์ด ์์ด.
1๏ธโฃ ์ ์ฝ์กฐ๊ฑด์ ํตํด ์ ๋๋ฉ์ด์ ์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ
์ ์ฝ์กฐ๊ฑด๋ ๋ฒํผ์ด๋ ๋ ์ด๋ธ ๋ฑ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก IBOulet์ ์ฐ๊ฒฐํ ์ ์๋๋ฐ ์ด๋ฅผ ์ด์ฉํด์ ์ง์ ์ปจํธ๋กค์ด ๊ฐ๋ฅํด.
โ ์ ๋๋ฉ์ด์ ์ด ์ ์์ ์ผ๋ก ์๋ํ๋ ค๋ฉด ์ฌ์ ์ค๋น๊ฐ ํ์ํด.
1. ๋ฉ๋ชจ๋ฆฌ์ ๋ทฐ๊ฐ ์ฌ๋ผ์์ ๋,
2. ๋ทฐ๊ฐ ํ๋ฉด์ ๋ณด์ฌ์ง ๋, (์ฃผ์์ ํํ์ด ์กฐ๊ธ ๋ชจํธํด)
- ์ฌ๊ธฐ์ ์ฃผ์ํ ์ ์ viewWillAppear์ ์ฌ์ฉํ๋ฉด, ๋ทฐ๊ฐ ์ฌ๋ผ์ค๊ธฐ ์ ์ ์ฝ๋๊ฐ ์คํํด์ ๋ด๊ฐ ์ํ๋ ๋ฐ์ ๋ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ด.
โ ์ ๊ทธ๋ผ ์ฌ์ ์ค๋น๊ฐ ๋๋ฌ๋ค๋ฉด ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ์์ ๋, ์ด๋ค ์ก์ ์ ์ทจํ๋์ง ์ฝ๋๋ก ํ๋ฒ ๋ณผ๊น?
private func prepareAnimation() {
nameLabelCenterX.constant = view.bounds.width // ํ์คํ๊ฒ ๋ทฐ์ ๋ฐ๊นฅ์ผ๋ก ๋ด๋ณด๋ด๊ธฐ ์ํด์
bountyLabelCenterX.constant = view.bounds.width // ํ์คํ๊ฒ ๋ทฐ์ ๋ฐ๊นฅ์ผ๋ก ๋ด๋ณด๋ด๊ธฐ ์ํด์
}
๋ฐ๊นฅ์์ ์ง๋ผ๋~ ํ๊ณ ๋ฑ์ฅํ๋ ์ฝ๋๋ผ์ ์ด๋ฐ ์ก์ ์ ์ทจํ๊ฒ ๋ผ.
์ฝ๋์ ๋ํด์ ๋ถ์ํด๋ณด๋ฉด, ์๊น ์ฐ๊ฒฐํ๋ ๋ค์๋ ์ด๋ธ๊ณผ ๋ฐ์ดํฐ๋ ์ด๋ธ์ ์ ์ฝ์กฐ๊ฑด์ ๋ทฐ์ ๋์ ์ ์์นํ๊ฒ ํด.
์ด๋ค ๋ง์ด๋๋ฉด ์ ๋ฐ ์์ผ๋ก ์์ฑํ๊ฒ ๋๋ฉด, ๋ทฐ์ width์ ํฌ๊ธฐ ์ง์ ์ label์ ์์์ง์ ์ด ์กํ๊ฒ ๋์ ์์ ๊ธ์๊ฐ ๋ฐ๊นฅ์ผ๋ก ๋๊ฐ๋ฒ๋ฆฌ๋ ํจ๊ณผ๊ฐ ์์ด.
โ ๊ทธ๋ผ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ์ค๊ณ ๋ ํ, ์ด์ ๋ ํ๋ฉด์ ๋ณด์ฌ์ง๋ ์ด๋ค ์ก์ ์ ์ทจํ๋์ง ๋ณผ๊น?
private func showAnimation() {
nameLabelCenterX.constant = 0
bountyLabelCenterX.constant = 0
/* ๊ฐ์ฅ ๊ฐ๋ตํ
UIView.animate(withDuration: 0.3) { // 0.3์ด๋์
self.view.layoutIfNeeded() // ๋ ์ด์์์ ๋ค์ ํด์ผํ ํ์๊ฐ ์๋ค๋ฉด ๋ค์ ํด๋ผ
}*/
/* ์กฐ๊ธ ๋ ์คํ ๋ค๋ํ
UIView.animate(withDuration: 0.3,
delay: 0.1,
options: .curveEaseIn,
animations : {
self.view.layoutIfNeeded()
}, completion: nil)
*/
// ๊ธ์๊ฐ ์คํ๋ง์ฒ๋ผ ํ๊ธฐ๋ ์ฝ๋
UIView.animate(withDuration: 0.5, delay: 0.2, usingSpringWithDamping: 0.6, initialSpringVelocity: 2, options: .allowUserInteraction, animations: self.view.layoutIfNeeded, completion: nil)
// ์นด๋๊ฐ ๋์๊ฐ
UIView.transition(with: imgView, duration: 0.3, options: .transitionFlipFromLeft, animations: nil, completion: nil)
}
์ฃผ๋ชฉํ ๋งํ ์ ์ผ๋ก๋ ๊ฐ ๋ ์ด๋ธ ์ ์ฝ์กฐ๊ฑด์ constant = 0๋ก ๋ฐ๊พผ๋ค๋ ์ ์ด์ผ.
์ด๊ฒ ์ด๋ค ์๋ฏธ์ด๋๋ฉด, ๊ธฐ์กด์ ์ค์ ๋์ด ์๋ ์ ์ฝ์กฐ๊ฑด์ผ๋ก ๋๋ ค๋๋ ๋ค๋ ๋ง์ด์ผ
- self.view.layoutIfNeeded() : ๋ ์ด์์์ ์กฐ์ ํ ํ์๊ฐ ์์ผ๋ฉด ์กฐ์ ํด๋ผ.
๋๋จธ์ง ์ฝ๋๋ ๊ทธ๋ฅ ๋ฑ ๋ณด๋ฉด ์ดํด๊ฐ ๊ฐ๊ฑฐ๋๊น ์์ฉํด ๋ณด๊ธฐ ๋ฐ๋ผ.
https://github.com/lgvv/fastCampus/tree/main/BountyList%20-%20Collection%2BAnimate
lgvv/fastCampus
Contribute to lgvv/fastCampus development by creating an account on GitHub.
github.com
----------------------------------------------------------------------------------------------------------------------
โ ์ฌ๊ธฐ๋ถํด ๋ทฐ์ ์์ฑ์ ์ด์ฉํ์ฌ ์ ๋๋ฉ์ดํธ!
์ฌ๊ธฐ๋ ์์ ๋ง์ฐฌ๊ฐ์ง๋ก viewDidLoad์ viewDidAppear ๋ถ๋ถ์ ๋๊ฐ์.
ํ.์ง.๋ง!!
โ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ผ์์๋ ์กฐ๊ธ์ ๋ค๋ฅด๊ฒ ์ฝ๋ฉํด์ผํด
private func prepareAnimation() {
nameLabel.transform = CGAffineTransform(translationX: view.bounds.width, y: 0).scaledBy(x: 3, y: 3).rotated(by: 180) // ์์น์ด๋ - ์ค์ผ์ผ์ 3๋ฐฐ - ํ์ ์ 180๋
bountyLabel.transform = CGAffineTransform(translationX: view.bounds.width, y: 0).scaledBy(x: 3, y: 3).rotated(by: 180) // ์์น์ด๋ - ์ค์ผ์ผ์ 3๋ฐฐ - ํ์ ์ 180๋
nameLabel.alpha = 0
bountyLabel.alpha = 0
}
์ฌ์ ์ค๋น ํ๋ ๋ถ๋ถ์ด ์กฐ๊ธ ๋ฌ๋ผ.
๊ฐ๋ ํธ์์ ๋ณด์๋ค ์ถ์ด transform์ ์ด์ฉํ ๊ฑฐ์ผ.
CGAffineTransform์ ์ด์ฉํด์
translation(์ด๋)์ ํ์ฉํด์ ์์น๋ฅผ y์ถ์ ๊ทธ๋๋ก ๋๊ณ x์ถ์ ๋ณ๊ฒฝํด์ ํ๋ฉด ๋ฐ์ผ๋ก ๋ด๋ณด๋ด
scaledBy(ํ๋)๋ฅผ ์ด์ฉํด์ x์y ๊ฐ์ 3๋ฐฐ ํฌ๊ฒ ๋ง๋ ํ์
rotated(ํ์ )์ ์ด์ฉํด์ 180๋ ํ์ ์ ๋ง๋ค์ด.
๊ทธ ๋ค์์ alpha ๊ฐ์ ์ฃผ์ด์ ํฌ๋ช ํ๊ฒ ๋ง๋๋๊ฒ ์ด๊ธฐ ์์ ์ด์ผ.
โ ๊ทธ๋ผ ์ด์ ํ๋ฉด์ ๋ํ๋๊ธฐ ์ ์ ์ด๋ ํ ์ก์ ์ ์ทจํ๋์ง ๋ณผ๊น?
private func showAnimation() {
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 2, options: .allowUserInteraction, animations: {
self.nameLabel.transform = CGAffineTransform.identity // ๋ณํ์ด ์๋ ๋ชจ์ต์ ์ ๋ณด๋ก
self.nameLabel.alpha = 1
}, completion: nil)
UIView.animate(withDuration: 1, delay: 0.2, usingSpringWithDamping: 0.6, initialSpringVelocity: 2, options: .allowUserInteraction, animations: {
self.bountyLabel.transform = CGAffineTransform.identity
self.bountyLabel.alpha = 1
}, completion: nil)
UIView.transition(with: imgView, duration: 0.3, options: .transitionFlipFromLeft, animations: nil, completion: nil)
}
UIView API๋ฅผ ์ด์ฉํ์ฌ ์ ๋๋ฉ์ด์ ์ ์ค๊ฑด๋ฐ ํ๋ผ๋ฏธํฐ๋ ์ฝ์ด๋ณด๋ฉด ์ฝ๊ฒ ํ์ ํ ์ ์์๊ฑฐ์ผ.
๋ช๊ฐ์ง ์ฃผ๋ชฉํ ๋งํ ์ ์
1. ์ ๋๋ฉ์ดํธ๋ฅผ ๋ถ๋ฆฌํด์ ๊ฐ๊ฐ์ ๋ ์ด๋ธ์ ๋ค๋ฅธ ํจ๊ณผ๋ฅผ ์ฃผ์ด์ ๋ ์์ฑ๋ ์๊ฒ ๋ง๋ค์๋ค.
2. CGAffineTransform.identity : ์ฒ์์ ๋ณํ์ด ์๋ ๋ชจ์ต์ ์ ๋ณด๋ก
https://github.com/lgvv/fastCampus/tree/main/BountyList%20-%20Collection%20%2B%20Animate2
lgvv/fastCampus
Contribute to lgvv/fastCampus development by creating an account on GitHub.
github.com
์ฐธ๊ณ
'โ ๏ธ deprecated โ ๏ธ > ํจ์บ (์ฌ์ธ์)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฆง ch13 Static Cell (TableView) (0) | 2021.06.25 |
---|---|
ch12 ์ ํ๋ฎค์งst ์์ ์ฑ ์ฝ๋๋ฆฌ๋ทฐ (0) | 2021.06.24 |
ch11 ํ์๊ธ ๋ญํน์ฑ ์ฝ๋๋ฆฌ๋ทฐ(CollectionView) (0) | 2021.06.22 |
๐ ch11 Animation - ๊ฐ๋ ํธ๐ (0) | 2021.06.22 |
ch10 ํ์๊ธ ๋ญํน์ฑ ์ฝ๋๋ฆฌ๋ทฐ(MVVM) (0) | 2021.06.21 |