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

[iOS/UI]ํƒญ๋ฐ” ์•„์ดํ…œ ๋ˆŒ๋ €์„ ๋•Œ ์• ๋‹ˆ๋ฉ”์ด์…˜ ๋„ฃ๊ธฐ (UITabBarItem Animation)

by Fomagran ๐Ÿ’ป 2020. 10. 28.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!!!

 

์˜ค๋Š˜ ์•Œ์•„๋ณผ ๊ฑด ํƒญ๋ฐ”๋ฅผ ํƒญํ–ˆ์„ ๋•Œ ์ด๋ฏธ์ง€๊ฐ€ ์Šคํ”„๋ง์ฒ˜๋Ÿผ ์ปค์กŒ๋‹ค ์ž‘์•„์กŒ๋‹คํ•˜๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ ์šฉํ•ด๋ณผ๊ฑฐ์—์š”.

 

๊ทธ๋Ÿผ ๋ฐ”๋กœ ์‹œ์ž‘ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

Xcode


 

์šฐ์„  ํƒญ๋ฐ” ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”.

 

override๋œ didSelect(ํƒญ๋ฐ”์•„์ดํ…œ ์„ ํƒํ–ˆ์„์‹œ) ๋ฉ”์†Œ๋“œ๋ฅผ ์จ์ค๋‹ˆ๋‹ค.

 

์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ์•ˆ์— ์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ๋„ฃ์Šต๋‹ˆ๋‹ค.(์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•„๋‚ด๋Š” ์ฝ”๋“œ)

๊ทธ ๋‹ค์Œ ์• ๋‹ˆ๋ฉ”์ด์…˜ ํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ๊ตฌํ˜„ํ•ด์ค๋‹ˆ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ didSelectํ–ˆ์„ ๋•Œ performSpringAnimationํ•จ์ˆ˜์— imageView๋ฅผ ์ธ์ž๊ฐ’์œผ๋กœ ๋„ฃ์–ด์ค€ ๋’ค ์‹คํ–‰ํ•ด์ค๋‹ˆ๋‹ค.

StoryBoard


์Šคํ† ๋ฆฌ๋ณด๋“œ๋กœ ์ด๋™ํ•˜์…”์„œ ํƒญ๋ฐ”์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋งŒ๋“ค๊ณ  ๊ฐ ์•„์ดํ…œ์— ์ด๋ฏธ์ง€๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. (์ €๋ ‡๊ฒŒ ์›๋ณธ ๊ทธ๋Œ€๋กœ์˜ ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ํ•˜์‹œ๊ณ  ์‹ถ์œผ๋ฉด

์—ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”)

๋ฐ˜๋“œ์‹œ ํด๋ž˜์Šค ์ด๋ฆ„์— ์•„๊นŒ ๋งŒ๋“ค์–ด์ฃผ์…จ๋˜ ํƒญ๋ฐ”์ปจํŠธ๋กค๋Ÿฌ๋ž‘ ๊ฐ™์€ ์ด๋ฆ„์„ ์จ์•ผํ•ฉ๋‹ˆ๋‹ค.

GIF


์‹คํ–‰ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์•„์ดํ…œ์„ ํƒญํ–ˆ์„ ๋•Œ ํŠ€์–ด์˜ค๋ฅด๋Š” ๋“ฏํ•œ ์• ๋‹ˆ๋ฉ”์ด์…˜์ด ์ ์šฉ๋œ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!!

 

Source Code


์ „์ฒด์†Œ์Šค์ฝ”๋“œ

 

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
32
33
34
import UIKit
 
class TabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        guard let idx = tabBar.items?.firstIndex(of: item), tabBar.subviews.count > idx + 1let imageView = tabBar.subviews[idx + 1].subviews.compactMap ({ $0 as? UIImageView }).first else {
                  return
              }
        
        performSpringAnimation(imgView: imageView)
    }
    
    //์ด๋ฏธ์ง€๋ทฐ์— ์Šคํ”„๋ง ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ ์šฉํ•˜๊ธฐ
    func performSpringAnimation(imgView: UIImageView) {
        
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
            
            imgView.transform = CGAffineTransform.init(scaleX: 1.4, y: 1.4)
            
            UIView.animate(withDuration: 0.5, delay: 0.2, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
                imgView.transform = CGAffineTransform.init(scaleX: 1, y: 1)
            }) { (flag) in
            }
        }) { (flag) in
            
        }
    }
    
}

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€