Review of Implement a Custom Control (2) - Food Tracker
Add Support for Interface Builder
@IBDesignable
Interface Builder๋ view์ ์ฐ๊ฒฐ๋ ์ปค์คํ ์ฝ๋์ ๋ํด์ ์ ํ ๋ชจ๋ฆ ๋๋ค.
๊ทธ๋์ ์ปค์คํ ์ฝ๋๋ฅผ ๋ฐ์ํ์ง ์์ ์ฑ ์ธ์คํด์คํํ์ฌ canvas์ ๊ทธ๋ฆฝ๋๋ค.
์ปค์คํ ์ฝ๋๋ฅผ ๋ฐ์ํ๊ฒ ํ๋ ๋ฐฉ๋ฒ์ custom control code ์์ @IBDesignable attribute ๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ ๋๋ค.
์ด Attribute ์ ํตํด Interface Builder ๊ฐ canvas์ view๋ฅผ ๊ทธ๋ฆด๋, ์ปค์คํ ์ฝ๋๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ๋ฐ์ํ์ฌ ๊ทธ๋ฆด ์ ์๊ฒ ํด์ค๋๋ค.
" This lets Interface Builder instantiate and draw a copy of your control directly in the canvas."
์ถ๊ฐ์ ์ผ๋ก, Interface Builder ๊ฐ custom control์ ์์ํ ์นดํผ๋ณธ์ ๊ฐ์ง๊ณ ์์ผ๋, ๊ทธ๊ฒ์ layout engine ์ด control์ ์์น์ ์ฌ์ด์ฆ๋ฅผ ์ ์ ํ๊ฒ ์ค์ ํ ์ ์๊ฒ ๋ฉ๋๋ค.
์์ฝ: @IBDesignable์ ์ถ๊ฐํ๋ฉด canvas์์ ์ปค์คํ ์ฝ๋์ ์ ์ฉ์ฌํญ์ ํ์ธํ ์ ์์ต๋๋ค. ( ์ถ๊ฐํ์ง ์์ผ๋ฉด ๋ฐํ์์์๋ง ๊ฐ๋ฅ )
@IBInspectable
@IBInspectable attribute๋ฅผ control์ ์์ฑ ์์ ์ถ๊ฐํ๋ฉด, Attribute Inspector์ ์ถ๊ฐํ ์์ฑ์ด ๋ํ๋๊ฒ ๋ฉ๋๋ค.
// StackView Custom Code `RatingControl.swift` ์ค
@IBInspectable
var starSize: CGSize = CGSize(width: 44.0, height: 44.0) {
didSet {
setupButtons()
}
}
@IBInspectable
var starCount: Int = 5 {
didSet {
setupButtons()
}
}
ํ์ง๋ง Attribute Inspector์์ ๋ณ๊ฒฝํ ์์ฑ์ ์ ์ฉ๋์ง ์์ ๊ฒ์ ๋๋ค.
์ฑ ๋์์ธ ์์ ์ canvas ์์๋ ๋ฐํ์ ๋ชจ๋ ์ ์ฉ๋์ง ์์ต๋๋ค.
๋ณ๊ฒฝํ ์์ฑ ๊ฐ์ control์ ์ ์ฉํ๊ธฐ ์ํด์๋ ๋ณ๊ฒฝ ์๋ง๋ค control์ ๋ฆฌ์ ํด์ฃผ์ด์ผ ํฉ๋๋ค.
์์ฑ ๊ฐ์ ๋ณ๊ฒฝ์๋ง๋ค ํธ์ถ๋๋ Property observer๋ฅผ ์ถ๊ฐํ๋ฉด ๋ฉ๋๋ค. ( ๊ทธ์ค `didSet` )
" This code iterates over all of the rating control’s buttons.
First, it removes the button from the list of views managed by the stack view.
This tells the stack view that it should no longer calculate the button’s size and position—but the button is still a subview of the stack view. "
์ ๋ฌธ์ฅ์์ ArrangedSubView์ ์ถ๊ฐ๋๋ ๊ฒ๊ณผ SubView์ ์ถ๊ฐ๋๋ ๊ฒ์ ๋ณ๊ฐ๋ผ๋ ๊ฒ์ ์๊ฒ ๋์์ต๋๋ค.
ArrangedSubView ๋ Subview์ ๋ถ๋ถ์งํฉ์ผ๋ก์, SubView์ ์ถ๊ฐ๋๋ ๊ฒ์ด ArrangedSubView ์ถ๊ฐ๋๋ ๊ฒ์ด๋ผ๊ณ ๋ณผ ์ ์์ต๋๋ค.
๋ ์์ธํ ์ดํด๋ณด๋ฉด, ArrangedSubView์ ์ถ๊ฐ๋๋ฉด Stack View๋ ์ด ์์ดํ ์ size์ position์ ๊ณ์ฐํด์ผ ํ๋ค๋ ๊ฒ์ ์๊ฒ ๋์์ต๋๋ค.
๊ด๋ จ ๊ธ 1: arranged subview vs subview- stackoverflow
๊ด๋ จ ๊ธ 2: arranged subview - apple developer documentation
N O T E
setUpButton( ) ์ ๋ก์ง์ด ํผํฌ๋จผ์ค ๊ด์ ์์ ์ต๊ณ ์ ์์ด๋์ด๋ ์๋์ง๋ง, ํด๋น ๋ฉ์๋๋ Interface Builder์ ์ํด ์ฑ design time์ ํธ์ถ๋ ๋ฟ์ด๊ณ , ์ฑ ์คํ ์์๋ ๋จ ํ๋ฒ ํธ์ถ๋๊ธฐ ๋๋ฌธ์ ๋ฌธ์ ๊ฐ ๋์ง ์์ต๋๋ค.
Add Star Images to the Buttons
Assets.xcassets ํ์ผ์ ์๋ก์ด ํด๋๋ฅผ ๋ง๋ค๊ณ 3๊ฐ์ง ์ํ์ ๋ณ ์ด๋ฏธ์ง๋ฅผ ์ถ๊ฐํฉ๋๋ค.
Assets.xcassets ๋ asset catalog๋ฅผ ๋ด๊ณ ์๋ ํ์ผ์ ๋๋ค. Asset catalog๋ ์ด๋ฏธ์ง๋ค์ ์ ์ฅํ๊ณ ๊ด๋ฆฌํ๋ ์ฅ์์ ๋๋ค.
To set star images for the buttons
Assets catalog ๊ฐ ์ฑ์ main bundle์ ์์นํด์๋ค๋ ๊ฒ์ ์๊ธฐํ์ธ์
์ด๊ฒ์ ์ด๋ฏธ์ง๋ฅผ UIImage(named:) ๋ฉ์๋๋ก ๋ถ๋ฌ์ฌ ์ ์๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
ํ์ง๋ง control ์ด @IBDesignable ์ด๊ธฐ ๋๋ฌธ์, ๊ตฌ์ฑ ์ฝ๋๋ ์ธํฐํ์ด์ค ๋น๋์์๋ ์คํ๋ ํ์๊ฐ ์์ต๋๋ค.
์ด๋ฏธ์ง๋ฅผ ์ธํฐํ์ด์ค ๋น๋์์ ์ ์ ํ๊ฒ ๋ถ๋ฌ์ค๊ธฐ ์ํด์๋, ๋ฐ๋์ catalog์ bundle์ ํน์ ํ์ฌ ๋ช ์ํด์ฃผ์ด์ผ ํฉ๋๋ค.
์ด๊ฒ์ด ์์คํ ์ด ์ด๋ฏธ์ง๋ฅผ ์ฐพ๊ณ ๋ถ๋ฌ์ค๋ ๊ฒ์ ๋ณด์ฅํด์ค๋๋ค.
๊ด๋ จ ๊ธ: Bundle
UIButton์ ํน์ง
-
5๊ฐ์ง ์ํ๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค. ( normal, highlighted, focused, selected, disabled )
-
๊ธฐ๋ณธ์ ์ผ๋ก ์ํ์ ์ํด ๋ชจ์ต์ด ๋ฐ๋๋๋ค.
-
๋ฒํผ์ ๋์์ ์ฌ๋ฌ ๊ฐ์ง ์ํ์ผ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค๋ฉด disabled ์ด๋ฉด์ highlighted ์ผ ์ ์์ต๋๋ค.
-
์ฌ์ฉ์๊ฐ ๋ฒํผ์ ํฐ์นํ๋ฉด ๋ฒํผ์ ์ํ์ ์๊ด์์ด ํญ์ `highligted ` ์ํ๊ฐ ๋ฉ๋๋ค.
-
focused ์ํ๋ focus ๊ธฐ๋ฐ ์ธํฐํ์ด์ค์์ ์ฌ์ฉ๋ฉ๋๋ค. ( Apple TV์ ๊ฐ์ )
Implement the Button Action
์ ํ๋ Button์ ๋ฐฐ์ด ๋ด ์ธ๋ฑ์ค๋ฅผ ์ฐพ๋ ๊ตฌํ์์ index(of:) ๋ฉ์๋๋ deprecated ๋์์ผ๋ฏ๋ก, firsrtIndex(of:) ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์์ต๋๋ค.
์ด์ธ๋ ๊ธฐ์กด์ ๋ฐฐ์ด ๋ด์ฉ์ ๋ณต์ต ์ด๋ฏ๋ก ์๋ตํ์์ต๋๋ค.
Add Accessibility Information
๊ธฐ๊ธฐ ์ฌ์ฉ์ ๋์์ด ํ์ํ ์ฌ๋๋ค์๊ฒ ํ๋ฅญํ ๋ชจ๋ฐ์ผ ๊ฒฝํ์ ์ ๊ณตํด ์ค ์ ์๋ ๋ฐฉ๋ฒ์ ๋๋ค.
VoiceOver, Switch Control, Guided Access ๋ฑ์ ํฌํจํฉ๋๋ค.
ํนํ VoiceOver๋ ์ ์๋ ฅ์ ๋ฐ ์๊ฐ์ฅ์ ์ธ์๊ฒ ํ๋ฅญํ ์คํฌ๋ฆฐ๋ฆฌ๋๊ฐ ๋ฉ๋๋ค.
VoicdeOver๋ ์ฌ์ฉ์์๊ฒ ๋น์ ์ UI๋ฅผ ์ฝ์ด ์ค๋๋ค.
๊ธฐ์กด์ control ๋ค ์๊ฒ๋ ํ๋ฅญํ ๊ธฐ๋ณธ ์ค๋ช ์ด ๋์ด ์์ง๋ง, custom view์ control๋ค์ ๋ํด์๋ ์ต์ ํ๊ฐ ํ์ํฉ๋๋ค.
์ดํ Accessibility์ ๋ํ ๋ด์ฉ์ ๋ฐ๋ก ๊ธ๋ก ์์ฑํ๊ฒ ์ต๋๋ค.
Connect the Rating Control to the View Controller
๊ธฐ์กด์ ๋ฐฐ์ด ๋ด์ฉ์ ์ด์ฉํด ์ค์ตํ๋ ๋ด์ฉ์ด๋ฏ๋ก ์๋ตํ์์ต๋๋ค.
Clean Up the Project
ํด๋ฆฐ ๋น๋ ํ๋ ๋ฒ : Command-Shift-K ์ดํ ์ฌ๋น๋
๋!