๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป/iOS

UIButton, UISlider, UILabel์ด๋ž€?

reujusong 2020. 7. 7. 15:06
๋ณธ ๊ฒŒ์‹œ๊ธ€์€ edwith ๋ถ€์ŠคํŠธ์ฝ”์Šค iOS ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ์ž‘์„ฑํ•œ ๊ฒŒ์‹œ๊ธ€ ์ž…๋‹ˆ๋‹ค.

 

UIButton, UISlider, UILabel

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ™”๋ฉด์„ ๊ตฌํ˜„ํ•  ๋•Œ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” UI์š”์†Œ๋“ค์ด๋‹ค.

 


1. UIButton

์‚ฌ์šฉ์ž์˜ ์ƒํ˜ธ์ž‘์šฉ(ํ„ฐ์น˜/ํƒญ ๋“ฑ์˜ ์ด๋ฒคํŠธ)์— ๋ฐ˜์‘ํ•ด ๋ฏธ๋ฆฌ ์ง€์ •๋œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์ปจํŠธ๋กค ์š”์†Œ

 

 

๋ฒ„ํŠผ ์ƒ์„ฑ์˜ 3๋‹จ๊ณ„

  1. ๋ฒ„ํŠผ์„ ์ƒ์„ฑํ•˜๊ณ  ๋ฒ„ํŠผ์˜ ์œ ํ˜•์„ ์„ ํƒ
  2. ๋ฒ„ํŠผ์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•œ ๋ฌธ์ž(ํƒ€์ดํ‹€)์„ ์ž…๋ ฅํ•˜๊ฑฐ๋‚˜, ์ด๋ฏธ์ง€๋ฅผ ์„ค์ •ํ•œ ๋’ค ํฌ๊ธฐ๋ฅผ ์กฐ์ •
  3. ๋ฒ„ํŠผ์— ํŠน์ • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ ์ž‘๋™ํ•  ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์—ฐ๊ฒฐ

 

๋ฒ„ํŠผ๊ณผ ๋ฉ”์„œ๋“œ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•

 

  1. addTarget(_:action:for:) ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
  2. ์ธํ„ฐํŽ˜์ด์Šค ๋นŒ๋”์—์„œ ์—ฐ๊ฒฐ (@IBAction)

 

์ด๋•Œ ๋ฒ„ํŠผ๊ณผ ์—ฐ๊ฒฐ๋˜๋Š” ๋ฉ”์„œ๋“œ ํ˜•์‹์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

 

func doSomething() func doSomething(sender: UIButton) 
func doSomething(sender: UIButton, forEvent event: UIEvent)

 

๋ฒ„ํŠผ์˜ ์ƒํƒœ: default, highlighted, focused, selected, disabled

 

๋ฒ„ํŠผ์˜ ์ƒํƒœ๋Š” ์กฐํ•ฉ๋œ ์ƒํƒœ์ผ ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ) [default + highlighted], [selected + disabled] ๋“ฑ๋“ฑ

๋ฒ„ํŠผ ์ƒ์„ฑ ์‹œ ๊ธฐ๋ณธ ์ƒํƒœ ๊ฐ’์€ default์ด๋ฉฐ, ์‚ฌ์šฉ์ž๊ฐ€ ๋ฒ„ํŠผ๊ณผ ์ƒํ˜ธ์ž‘์šฉ์„ ํ•˜๋ฉด ์ƒํƒœ ๊ฐ’์ด ๋ณ€ํ•˜๊ฒŒ ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ , ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹ ํ˜น์€ ์ธํ„ฐํŽ˜์ด์Šค ๋นŒ๋”๋ฅผ ์ด์šฉํ•ด ๋ฒ„ํŠผ์˜ ๊ฐ ์ƒํƒœ์— ๋Œ€ํ•œ ์†์„ฑ์„ ๋ณ„๋„๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋งŒ์•ฝ ๋ณ„๋„๋กœ ์†์„ฑ์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด UIButton ํด๋ž˜์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋ณธ ๋™์ž‘์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค.

์˜ˆ) disabled ๋ฒ„ํŠผ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ํ๋ฆฌ๊ฒŒ ํ‘œ์‹œ๋˜๋ฉฐ ์‚ฌ์šฉ์ž๊ฐ€ ํƒญ ํ•ด๋„ highlighted ๋˜์ง€ ์•Š๋Š”๋‹ค.

 

๋ฒ„ํŠผ์˜ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ์‹์—๋Š” ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์Šคํ† ๋ฆฌ๋ณด๋“œ์˜ ์ธ์ŠคํŒฉํ„ฐ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

 

enum UIButtonType: ๋ฒ„ํŠผ์˜ ์œ ํ˜•

๋ฒ„ํŠผ์˜ ์œ ํ˜•์— ๋”ฐ๋ผ ๋ฒ„ํŠผ์˜ ๊ธฐ๋ณธ์ ์ธ ์™ธํ˜•๊ณผ ๋™์ž‘์ด ๋‹ฌ๋ผ์ง„๋‹ค. ์ฒ˜์Œ ๋ฒ„ํŠผ์„ ์ƒ์„ฑํ•  ๋•Œ

 

  1. init(type:) ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉ ํ˜น์€
  2. ์ธํ„ฐํŽ˜์ด์Šค๋นŒ๋”์˜ “Attribute Inspector”์—์„œ ๋ฒ„ํŠผ ์œ ํ˜•์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค

์ด๋•Œ ํ•œ๋ฒˆ ์ƒ์„ฑ๋œ ๋ฒ„ํŠผ์˜ ์œ ํ˜•์€ ์ดํ›„ ๋ณ€๊ฒฝ ํ•  ์ˆ˜ ์—†์œผ๋ฉฐ, ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ์œ ํ˜•์€ Custom๊ณผ System์ด์ง€๋งŒ ํ•„์š”์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ์œ ํ˜•(Detail Disclosure, Info Light, Info Dark, Add Contact)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

  • var titleLabel: UILabel?: ๋ฒ„ํŠผ ํƒ€์ดํ‹€ ๋ ˆ์ด๋ธ”
  • var imageView: UIImageView?: ๋ฒ„ํŠผ์˜ ์ด๋ฏธ์ง€ ๋ทฐ
  • var tintColor: UIColor!: ๋ฒ„ํŠผ ํƒ€์ดํ‹€๊ณผ ์ด๋ฏธ์ง€์˜ ํ‹ดํŠธ ์ปฌ๋Ÿฌ

 

๋ฒ„ํŠผ์˜ ์ฃผ์š” ๋ฉ”์„œ๋“œ

// ํŠน์ • ์ƒํƒœ์˜ ๋ฒ„ํŠผ์˜ ๋ฌธ์ž์—ด ์„ค์ •
func setTitle(String?, for: UIControlState)

// ํŠน์ • ์ƒํƒœ์˜ ๋ฒ„ํŠผ์˜ ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜
func title(for: UIControlState) -> String?

// ํŠน์ • ์ƒํƒœ์˜ ๋ฒ„ํŠผ ์ด๋ฏธ์ง€ ์„ค์ •
func setImage(UIImage?, for: UIControlState)

// ํŠน์ • ์ƒํƒœ์˜ ๋ฒ„ํŠผ ์ด๋ฏธ์ง€ ๋ฐ˜ํ™˜
func image(for: UIControlState) -> UIImage?

// ํŠน์ • ์ƒํƒœ์˜ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ด๋ฏธ์ง€ ์„ค์ • 
func setBackgroundImage(UIImage?, for: UIControlState)

// ํŠน์ • ์ƒํƒœ์˜ ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ด๋ฏธ์ง€ ๋ฐ˜ํ™˜
func backgroundImage(for: UIControlState) -> UIImage?

// ํŠน์ • ์ƒํƒœ์˜ ๋ฌธ์ž์—ด ์ƒ‰์ƒ ์„ค์ •
func setTitleColor(UIColor?, for: UIControlState)

// ํŠน์ • ์ƒํƒœ์˜ attributed ๋ฌธ์ž์—ด ์„ค์ •
func setAttributedTitle(NSAttributedString?, for: UIControlState)

 

2. UILabel

ํ•œ์ค„ ๋˜๋Š” ์—ฌ๋Ÿฌ์ค„์˜ ํ…์ŠคํŠธ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋ทฐ๋กœ, UIButton๋“ฑ์˜ ์ปจํŠธ๋กค์˜ ๋ชฉ์ ์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.

 

๋ ˆ์ด๋ธ” ์ƒ์„ฑ์˜ 3๋‹จ๊ณ„

  1. ๋ ˆ์ด๋ธ”์„ ์ƒ์„ฑ
  2. ๋ ˆ์ด๋ธ”์ด ํ‘œ์‹œํ•  ๋ฌธ์ž์—ด์„ ์ œ๊ณต
  3. ๋ ˆ์ด๋ธ”์˜ ๋ชจ์–‘ ๋ฐ ํŠน์„ฑ์„ ์„ค์ •

 

3. UISlider

์—ฐ์†๋œ ๊ฐ’ ์ค‘์—์„œ ํŠน์ • ๊ฐ’์„ ์„ ํƒํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ปจํŠธ๋กค

 

์Šฌ๋ผ์ด๋” ์ƒ์„ฑ์˜ 3๋‹จ๊ณ„

  1. ์Šฌ๋ผ์ด๋”๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์Šฌ๋ผ์ด๋”๊ฐ€ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ ์ง€์ •
  2. ์ ์ ˆํ•œ ์ƒ‰์ƒ๊ณผ ์ด๋ฏธ์ง€๋ฅผ ์ด์šฉํ•ด ์Šฌ๋ผ์ด๋”์˜ ๋ชจ์–‘์„ ๊ตฌ์„ฑ
  3. ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์Šฌ๋ผ์ด๋”์™€ ์—ฐ๊ฒฐ

์‚ฌ์šฉ์ž ์ƒํ˜ธ์ž‘์šฉ์— ๋ฐ˜์‘ํ•˜๊ธฐ


์‚ฌ์šฉ์ž๊ฐ€ ์Šฌ๋ผ์ด๋”์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋ฉด ์Šฌ๋ผ์ด๋”์— ์—ฐ๊ฒฐ๋œ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜์–ด ์›ํ•˜๋Š” ์ž‘์—…์ด ์‹คํ–‰
๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์Šฌ๋ผ์ด๋”์˜ Thumb๋ฅผ ์ด๋™์‹œํ‚ค๋ฉด ์—ฐ์†์ ์œผ๋กœ ์ด๋ฒคํŠธ๋ฅผ ํ˜ธ์ถœํ•˜์ง€๋งŒ, isContinous ํ”„๋กœํผํ‹ฐ๊ฐ’์„ false๋กœ ์„ค์ •ํ•˜๋ฉด ์Šฌ๋ผ์ด๋”์˜ Thumb์—์„œ ์†์„ ๋–ผ๋Š” ๋™์‹œ์— ์ด๋ฒคํŠธ๋ฅผ ํ˜ธ์ถœ

 

์Šฌ๋ผ์ด๋”์™€ ๋ฉ”์„œ๋“œ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•

 

  • addTarget(_:action:for:) ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ
  • ์ธํ„ฐํŽ˜์ด์Šค ๋นŒ๋”์—์„œ ์—ฐ๊ฒฐ (@IBAction)

์Šฌ๋ผ์ด๋”์™€ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฉ”์„œ๋“œ ํ˜•์‹

์Šฌ๋ผ์ด๋”์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ–ˆ์„ ๋•Œ ํ•„์š”ํ•œ ์ •๋ณด์— ๋”ฐ๋ผ ์•„๋ž˜ ์„ธ ๊ฐ€์ง€ ์ค‘ ํ•œ ๊ฐ€์ง€๋ฅผ ์„ ํƒํ•˜์—ฌ ์‚ฌ์šฉ

 

func doSomething()
func doSomething(sender: UISlider)
func doSomething(sender: UISlider, forEvent event: UIEvent)

 

์Šฌ๋ผ์ด๋” ์ฃผ์š” ํ”„๋กœํผํ‹ฐ

 

์Šฌ๋ผ์ด๋”์˜ ํ”„๋กœํผํ‹ฐ ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ์‹์—๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋ฐฉ์‹๊ณผ, ์Šคํ† ๋ฆฌ๋ณด๋“œ์˜ ์ธ์ŠคํŽ™ํ„ฐ๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

  • var minimumValue: Float, var maximumValue: Float: ์Šฌ๋ผ์ด๋” ์–‘๋๋‹จ์˜ ๊ฐ’
  • var value: Float: ์Šฌ๋ผ์ด๋”์˜ ํ˜„์žฌ ๊ฐ’
  • var isContinuous: Bool: ์Šฌ๋ผ์ด๋”์˜ ์—ฐ์†์ ์ธ ๊ฐ’ ๋ณ€ํ™”์— ๋”ฐ๋ผ ์ด๋ฒคํŠธ ์—ญ์‹œ ์—ฐ์†์ ์œผ๋กœ ํ˜ธ์ถœํ•  ๊ฒƒ์ธ์ง€์˜ ์—ฌ๋ถ€
  • var minimumValueImage: UIImage?, var maximumValueImage: UIImage?: ์Šฌ๋ผ์ด๋” ์–‘๋๋‹จ์˜ ์ด๋ฏธ์ง€
  • var thumbTintColor: UIColor?: thumb์˜ ํ‹ดํŠธ ์ƒ‰์ƒ
  • var minimumTrackTintColor: UIColor?, var maximumTrackTintColor: UIColor?: thumb๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์•ž์ชฝ ํŠธ๋ž™๊ณผ ๋’ค์ชฝ ํŠธ๋ž™์˜ ํ‹ดํŠธ ์ƒ‰์ƒ

 

์Šฌ๋ผ์ด๋” ์ฃผ์š” ๋ฉ”์„œ๋“œ

 

// ์Šฌ๋ผ์ด๋”์˜ ํ˜„์žฌ ๊ฐ’ ์„ค์ •
func setValue(Float, animated: Bool)

// ํŠน์ • ์ƒํƒœ์˜ minimumTrackImage ๋ฐ˜ํ™˜
func minimumTrackImage(for: UIControlState) -> UIImage?

// ํŠน์ • ์ƒํƒœ์˜ minimumTrackImage ์„ค์ •
func setMinimumTrackImage(UIImage?, for: UIControlState)

// ํŠน์ • ์ƒํƒœ์˜ maximumTrackImage ๋ฐ˜ํ™˜
func maximumTrackImage(for: UIControlState) -> UIImage?

// ํŠน์ • ์ƒํƒœ์˜ minimumTrackImage ์„ค์ •
func setMaximumTrackImage(UIImage?, for: UIControlState)

// ํŠน์ • ์ƒํƒœ์˜ thumbImage ๋ฐ˜ํ™˜
func thumbImage(for: UIControlState) -> UIImage?

//ํŠน์ • ์ƒํƒœ์˜ thumbImage ์„ค์ •
func setThumbImage(UIImage?, for: UIControlState)

 

 

 

'๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป > iOS' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

UIKit๋ž€?  (0) 2020.07.12
Cocoa Touch ํ”„๋ ˆ์ž„์›Œํฌ๋ž€?  (0) 2020.07.08
AVFoundation, Timer๋ž€?  (0) 2020.07.08
IBOutlet, IBAction์ด๋ž€?  (0) 2020.06.26
์—์…‹ ์นดํƒˆ๋กœ๊ทธ๋ž€?  (0) 2020.06.26