๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
iOS ๐ŸŽ/iOS

Animation 1 - UIView Animation

by yongmin.Lee 2022. 3. 17.

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)

(๊ทธ๋ฆผ1) animatable property animation

๋ฐ˜๋ฉด Auto Layout์˜ Constraint ์†์„ฑ์€ Animatableํ•œ built-in animation ์†์„ฑ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— layoutIfNeeded๋ผ๋Š” ๋ฉ”์†Œ๋“œ์˜ ํ˜ธ์ถœ๋กœ ๊ทธ ์ฆ‰์‹œ ๊ฐ’์„ ๋ฐ˜์˜ํ•˜๋Š” ์ž‘์—…์„ ํ•ด์ฃผ๋Š” ์ˆ˜๋™์œผ๋กœ ๋ฐ˜๋“œ์‹œ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. layoutIfNeeded ๋ฉ”์†Œ๋“œ๋กœ frame์ด ๋™๊ธฐ์ ์œผ๋กœ ์ฆ‰์‹œ ๋ณ€๊ฒฝ๋จ์—๋”ฐ๋ผ ์• ๋‹ˆ๋ฉ”์ด์…˜ ์‹œ์Šคํ…œ์€ frame์˜ ๋งˆ์ง€๋ง‰ ์ƒํƒœ๋ฅผ ์•Œ์ˆ˜์žˆ๊ธฐ ๋•Œ๋ฌธ! (๊ทธ๋ฆผ2)

(๊ทธ๋ฆผ2)

์• ๋‹ˆ๋ฉ”์ด์…˜์€ ๋ทฐ์˜ ์‹œ์ž‘์ƒํƒœ์™€ ๋ ์ƒํƒœ๋ฅผ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ด๋ฏธ ์•Œ๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์ง€์†์‹œ๊ฐ„๊ณผ ํƒ€์ด๋ฐ ๊ณก์„ ์„ ์ด์šฉํ•˜์—ฌ ๊ทธ ์ค‘๊ฐ„ ์ƒํƒœ์˜ ๊ฐ’๋“ค์„ ๊ณ„์‚ฐํ•˜๋ฉด ์ค‘์ฒฉ๋œ(nested) ์• ๋‹ˆ๋ฉ”์ด์…˜ ๊ตฌ์กฐ๋ฅผ ํ†ตํ•ด ์ค‘๊ฐ„ ํ”„๋ ˆ์ž„๋“ค์„ ๋งŒ๋“ค์–ด ํ™”๋ฉด์— ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.(๊ทธ๋ฆผ3)

(๊ทธ๋ฆผ3)

ํ•˜์ง€๋งŒ ์ค‘์ฒฉ์ด ๋Š˜์–ด๋‚ ์ˆ˜๋ก ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๊ธฐ์— ์ด๋Ÿด๋•Œ๋Š” UIView.animate()๋Œ€์‹  UIView.animatekeyframes() ์‚ฌ์šฉํ•˜์—ฌ ์‹œ๊ฐ„ ์ˆœ์„œ๋Œ€๋กœ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ๋™์ž‘์‹œํ‚ฌ์ˆ˜ ์žˆ๋‹ค (๊ทธ๋ฆผ4)

(๊ทธ๋ฆผ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://nsios.tistory.com/167

https://jongwonwoo.medium.com/%EC%99%9C-%EC%95%A0%EB%8B%88%EB%A9%94%EC%9D%B4%EC%85%98-%EB%B8%94%EB%9F%AD%EC%97%90%EC%84%9C-layoutifneeded%EB%A5%BC-%EC%82%AC%EC%9A%A9%ED%95%A0%EA%B9%8C-6ac5280855e1

https://soooprmx.com/ios%EC%97%90%EC%84%9C-%EC%82%AC%EC%9A%A9%ED%95%A0-%EC%88%98-%EC%9E%88%EB%8A%94-%EC%95%A0%EB%8B%88%EB%A9%94%EC%9D%B4%EC%85%98-%EA%B5%AC%ED%98%84-%EA%B8%B0%EB%B2%95%EB%93%A4%EC%97%90-%EB%8C%80%ED%95%9C/

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