Skip to content

๐Ÿ“ฑiOS ํ† ๋ก  ๊ฑฐ๋ฆฌ

fElix edited this page Dec 1, 2020 · 20 revisions

iOS ํ† ๋ก ๊ฑฐ๋ฆฌ

์ฝ”๋”ฉ ์ปจ๋ฒค์…˜

  • ๊ธฐ๋ณธ์ ์œผ๋กœ ์ฝ”๋”ฉ์ปจ๋ฒค์…˜ ๋งํฌ ๋ฅผ ์ฐธ๊ณ ํ•˜๋˜, ๋ณ„๋„๋กœ ์ •๋ฆฌํ•  ํ•„์š”๊ฐ€ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐ๋˜๋ฉด ์•„๋ž˜์— ๊ธฐ์žฌํ•œ๋‹ค.
1. class, struct , protocol, extension ์€ ์„ ์–ธ ํ›„ ์ฒซ ์ค„ ๊ฐœํ–‰
class Example {

  let exampleNumber: Int
}
2. switch-case ๊ตฌ๋ฌธ์˜ ๊ฐ case ์‚ฌ์ด๋Š” ๊ฐœํ–‰

switch type {
  case .A:
    print("A ์ž…๋‹ˆ๋‹ค.")

  case .B:
    print("B ์ž…๋‹ˆ๋‹ค.")

  case .C:
    print("C ์ž…๋‹ˆ๋‹ค.")
}
3. ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ 3๊ฐœ ์ด์ƒ์ผ ์‹œ ๊ฐœํ–‰ํ•˜์—ฌ ์ž‘์„ฑํ•œ๋‹ค
init(
    window: UIWindow?,
    factory: CoordinatorFactoryable,
    signinChecker: SigninCheckable
  ) {
    self.window = window
    initCoordinatorFactory = factory
    self.signinChecker = signinChecker
  }
  1. CollectionView ์‚ฌ์šฉ์‹œ FlowLayout ์€ lazy var ๋ธ”๋Ÿญ ๋‚ด๋ถ€ (IBOutlet ์ผ์‹œ didSet) ์— ์„ ์–ธํ•œ๋‹ค.

ํ…Œ์ŠคํŠธ ๊ฐ€๋Šฅํ•œ ๊ตฌ์กฐ ๋งŒ๋“ค๊ธฐ

- MVVM-C ์•„ํ‚คํ…์ฒ˜ ํŒจํ„ด ์‚ฌ์šฉ
- ์ด์Šˆ๋‹น ํ•œ ๊ฐœ ์ด์ƒ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค ์ž‘์„ฑ
- ํ™”๋ฉด ์ „ํ™˜ ์‹œ ViewController ์— ์ƒ์„ฑ์ž๋กœ ์˜์กด์„ฑ ์ฃผ์ž…

View ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•?

- View์˜ ๋ชจ๋“  ์†์„ฑ์€ ์ฝ”๋“œ๋กœ ์„ค์ •
- ์ •์ ์ธ ๋ทฐ๋Š” Storyboard๋กœ ์ž‘์„ฑ
- ๋™์ ์ธ ๋ทฐ๋Š” ์ฝ”๋“œ๋กœ ์ž‘์„ฑ

๋””๋ ‰ํ† ๋ฆฌ

- Scene ๋‹จ์œ„ ๋ณ„๋กœ ๋ถ„๋ฆฌ

Coordinator ๊ตฌ์กฐ

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2020-11-19 แ„‹แ…ฉแ„’แ…ฎ 7 21 06


MVVM์ด ์•„๋‹Œ MVVM-C๋ฅผ ์„ ํƒํ•œ ์ด์œ 

- MVVM ์—์„œ ViewModel ์€ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„, View๋Š” presentation ๋ฐ presentation logic ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
- MVVM-C ๋ฅผ ํ†ตํ•ด ํ™”๋ฉด์„ ์ „ํ™˜ํ•˜๋Š” presentation logic ์„ Coordinator ๊ฐ€ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ํ•˜์—ฌ View ๋ฅผ ๊ฐ€๋ณ๊ฒŒ
  ๋งŒ๋“ค๊ธฐ ์œ„ํ•จ
- ๋˜ํ•œ ๊ฐ™์€ ํ™”๋ฉด์œผ๋กœ ์ „ํ™˜ํ•˜๊ณ  ์‹ถ์€ ๊ณณ์ด ๋งŽ์•„์ง€๋ฉด ํ•ด๋‹น Coordinator ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ™์€ ์ฝ”๋“œ๊ฐ€ ์—ฌ๋Ÿฌ ํด๋ž˜์Šค์—์„œ
  ๋ฐ˜๋ณต๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.
- ๋˜ํ•œ ์ „ํ™˜๋˜๋Š” ํ™”๋ฉด์„ ๋ณ€๊ฒฝํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ ์ฝ”๋“œ๋ฅผ ์ผ์ผ์ด ์ฐพ์„ ํ•„์š” ์—†์ด ํ•ด๋‹นํ•˜๋Š” Coordinator ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

Coordinator์˜ start()์—์„œ UIViewController return ํ•˜๋Š”๊ฒƒ์ด ๋งž๋Š”๊ฐ€?

- ํ•˜์œ„ Coordinate ๋กœ๋ถ€ํ„ฐ UIViewController ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜๋ฐ›๊ณ  ์ƒ์œ„ Coordinate ์—์„œ
  ์ด๋ฅผ present or push ํ•˜๋Š” ๊ฒƒ์ด ๋” ์ž์—ฐ์Šค๋Ÿฝ๋‹ค.
- ๊ทธ๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š์œผ๋ฉด ํ•˜์œ„ Coordinate ์—์„œ present or push ํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ตœ์ดˆ SceneCoordinate ์—
  present ๋‚˜ push ํ•  (์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”) Root UIViewController or UINavigationController ๋ฅผ
  ๊ฐ•์ œ๋กœ ๋„ฃ์–ด์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์†Œ ๋ถ€์ž์—ฐ์Šค๋Ÿฌ์›Œ์ง„๋‹ค.

openURL ์š”์ฒญ์„ ์–ด๋””์„œ ํ•  ๊ฒƒ์ธ๊ฐ€?

 - openURL ์„ ํ•˜๋ ค๋ฉด UIApplication.shared.open(url) ์„ ํ•ด์ค˜์•ผ ํ•˜๋Š”๋ฐ, ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด UIKit ์„
   import ํ•ด์•ผ ํ•จ Model ์ธ Router ์—์„œ UIKit ์„ import ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์€ ๋ถ€์ž์—ฐ์Šค๋Ÿฌ์šฐ๋ฏ€๋กœ ์ด๋ฅผ
   Presentation Logic ์œผ๋กœ ๊ฐ„์ฃผํ•˜์—ฌ Coordinator ์—์„œ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ํ•˜๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ๋‹ค.

Date ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•

์ฐธ๊ณ ๋งํฌ: ๋งํฌ

extension Date {
  
  func toString() -> String {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd"
    
    return dateFormatter.string(from: self)
  }
}

Touch Responder Chain ๊ณผ ๋ณ„๋„๋กœ ํŠน์ • View ๊ฐ€ ํ„ฐ์น˜์ด๋ฒคํŠธ๋ฅผ ๋ฐ›๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•

  • UITapGestureRecognizer ์ฐธ๊ณ ๋งํฌ

  • ํŠน์ • ์˜์—ญ์˜ ํ„ฐ์น˜์— ๋Œ€ํ•ด์„œ๋Š” UITapGestureRecognizer ์˜ ์ด๋ฒคํŠธ๋ฅผ ๋ชป ๋ฐ›๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    return touch.view?.isDescendant(of: commentView) == true ? false : true
}

CollectionView๊ฐ€ StackView์˜ ArrangedSubview๋กœ ์ถ”๊ฐ€๋˜์–ด ContentSize๊ฐ€ ์žกํžˆ์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•

  • UICollectionView๋ฅผ SubClassing ํ•˜์—ฌ ์•„๋ž˜์˜ ๋ณ€์ˆ˜๋ฅผ override ํ•˜๊ธฐ
    override var contentSize:CGSize {
      didSet {
        invalidateIntrinsicContentSize()
      }
    }
    
    override var intrinsicContentSize: CGSize {
      layoutIfNeeded()
      return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
    }
    • ํ•ด๋‹น collection view๋Š” autolayout์„ ์žก์ง€ ์•Š์•˜๋‹ค.
    • Apple ๊ณต์‹ ๋ฌธ์„œ์— ๋‚˜์˜ค๋Š” intrinsicContentSize๋ฅผ ์ฐพ์•„๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค๋ช…ํ•œ๋‹ค.
      • custom view๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋ ˆ์ด์•„์›ƒ ์‹œ์Šคํ…œ์ด ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜๋Š” content๋ฅผ ํ‘œ์‹œํ•œ๋‹ค. ์ด ์†์„ฑ์„ ์„ค์ •ํ•˜๋ฉด custom view๊ฐ€ ๋ ˆ์ด์•„์›ƒ ์‹œ์Šคํ…œ์— content๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์›ํ•˜๋Š” ํฌ๊ธฐ๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ณ€๊ฒฝ๋œ height์— ๋”ฐ๋ผ ๋ณ€๊ฒฝ๋œ width๋ฅผ ๋ฐฐ์น˜ ์‹œ์Šคํ…œ์— ๋™์ ์œผ๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ณธ์งˆ์ ์ธ ํฌ๊ธฐ๋Š” ์ปจํ…์ธ  ํ”„๋ ˆ์ž„๊ณผ ๋…๋ฆฝ์ ์ด์–ด์•ผ ํ•œ๋‹ค. custom view์— ์ง€์ •๋œ dimension์— ๋Œ€ํ•œ ๊ณ ์œ ํ•œ ํฌ๊ธฐ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น dimension์— ๋Œ€ํ•ด noIntrinsicMetric์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
    • Apple ๊ณต์‹ ๋ฌธ์„œ์— ๋‚˜์˜ค๋Š” invalidateIntrinsicContentSize() ๋ฅผ ์ฐพ์•„๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค๋ช…ํ•œ๋‹ค.
      • custom view์˜ intrinsicContentSize๋ฅผ ๋ฌดํšจํ™”ํ•˜๋Š” ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์žˆ์„ ๊ฒฝ์šฐ ์ด Method๋ฅผ ํ˜ธ์ถœํ•ด์•ผํ•œ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด constraint๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” layout์— ๋Œ€ํ•ด ์ƒˆ๋กœ์šด intrinsicContentSize๋ฅผ ํ—ˆ์šฉํ•˜๊ฒŒ ํ•œ๋‹ค.
    • Apple ๊ณต์‹๋ฌธ์„œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์œ„์˜ ์ฝ”๋“œ๋ฅผ ํ•ด์„ํ•ด๋ณด๋ฉด, contentSize๊ฐ€ ๋ณ€ํ•  ๊ฒฝ์šฐ invalidateIntrinsicContentSize()๋ฅผ ํ†ตํ•ด intrinsicContentSize property์— ๋งž๊ฒŒ view๋ฅผ ๊ฐฑ์‹ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.

๐ŸŽฏ ๊ณ„ํš


๐Ÿค ๊ทœ์น™


๐Ÿ‘ฉโ€๐ŸŽจ ์Šคํƒ€์ผ ๊ฐ€์ด๋“œ


๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆ ๋ฉ˜ํ† ๋‹˜๊ณผ์˜ ๋งŒ๋‚จ


๐Ÿ“œ ํ† ๋ก ๊ฑฐ๋ฆฌ


๐Ÿ“œ ๊ฐœ๋ฐœ ๋ฌธ์„œ


๐Ÿ’Ž ๊ธฐ์ˆ  ํŠน์žฅ์ 


๐Ÿƒโ€โ™‚๏ธ ์Šคํ”„๋ฆฐํŠธ ํšŒ์˜ / ํšŒ๊ณ 


๐Ÿ‰ ๋ฐ์ผ๋ฆฌ ์Šคํฌ๋Ÿผ


Clone this wiki locally