ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Review of Work with View Controllers (2) - Food Tracker
    iOS/๐Ÿค– App 2021. 1. 19. 23:44

    Create a Gesture Recognizer

    Image view๋Š” control ์ด ์•„๋‹™๋‹ˆ๋‹ค.

     

    ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— button ์ด๋‚˜ slider์™€ ๊ฐ™์ด ์‚ฌ์šฉ์ž์˜ input์— ๋ฐ˜์‘ํ•˜๋„๋ก ์„ค๊ณ„๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

     

    ๋”ฐ๋ผ์„œ ๋ฐ”๋กœ Control-drag ์„ ํ†ตํ•ด์„œ action method๋ฅผ ๋„ฃ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

    ์ด์ „ ๊ธ€์˜ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„ ( `Connet the Image View to Code` )์— ์ด ๋‚ด์šฉ์„ ๋‹ค๋ฃจ์—ˆ์Šต๋‹ˆ๋‹ค.

     

    View๋ฅผ control์ฒ˜๋Ÿผ ๋™์ž‘ํ•˜๊ฒŒ ํ•˜๋Š” ๋ฐฉ๋ฒ•์€, ํ•ด๋‹น View์— gesture recognizer๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

     

    Gesture recognizer ๋Š” view์— ๋ถ™์ด๋Š” ๊ฐ์ฒด๋กœ,  control๊ณผ ๊ฐ™์ด ์‚ฌ์šฉ์ž์˜ input์— ๋ฐ˜์‘ํ•˜๋„๋ก ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    ๊ฐ๊ฐ์˜ Gesture recognizer ๋Š” ๊ฐ์ž์— ์ƒ์‘ํ•˜๋Š” ๋™์ž‘์—๋งŒ ๋ฐ˜์‘ํ•ฉ๋‹ˆ๋‹ค. ( ๋™์ž‘์˜ ์˜ˆ:  swipe, pinch, rotation )

     

    ์ฆ‰, ํ•˜๋‚˜์˜ Gesture recognizer ์—๋Š” ํ•˜๋‚˜์˜ ๋™์ž‘์—๋งŒ ๋ฐ˜์‘ํ•˜๊ฒŒ ๋งคํ•‘๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

    " Gesture recognizer has its assigned gesture."

     

     

    Scene์˜ ์œ— ๋ถ€๋ถ„์„ `Scene Dock` ์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

    ์œ„ ์‚ฌ์ง„์˜ ์˜ค๋ฅธ์ชฝ Doument Outline ์—์„œ,

     

    !!! ์ €๋Š” Gesture recognizer๋ฅผ Image view์— ์ถ”๊ฐ€ํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— Image view ์•„๋ž˜ ๊ณ„์ธต์— ์ถ”๊ฐ€๋  ๊ฒƒ์ด๋ผ๊ณ  ์˜ˆ์ƒํ•˜์˜€์ง€๋งŒ,

    Content View์™€ ๋™์ผํ•œ ๊ณ„์ธต์— ์ถ”๊ฐ€๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์˜€์Šต๋‹ˆ๋‹ค.!!!

     

    ์ฒ˜์Œ์—๋Š” ์ž˜๋ชป ์ถ”๊ฐ€๋œ ์ค„ ์•Œ๊ณ  ํ—ค๋งค์—ˆ์ง€๋งŒ, Gesture Recognizer์™€ ์—ฐ๊ฒฐ๋œ view๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„ ํ•ด๊ฒฐํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

     

    Scene Dock ์—์„œ Gesture recognizer ๋ฅผ ์˜ค๋ฅธ์ชฝ ํด๋ฆญํ•˜์—ฌ ๋‚˜์˜ค๋Š” ์ฐฝ์˜ `Referencing Outlet Collecitons` ์—์„œ ์ œ๋Œ€๋กœ ์—ฐ๊ฒฐ ๋˜์—ˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

     


     

    Connect the Gesture Recognizer to Code

    action์„ ๋งŒ๋“œ๋Š” ๊ณผ์ •๊ณผ ์œ ์‚ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋žตํ•˜์˜€์Šต๋‹ˆ๋‹ค.

     

    ์ฐจ์ด์ ์€ Scene ๋‚ด์˜ view์—์„œ Control-drag ํ•˜์ง€ ์•Š๊ณ , Scene Dock์˜ Gesture recognizer์—์„œ Control-drag ํ•œ๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค.

    ํ˜น์€ Document Outline์—์„œ Control-drag ํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ถ€๋ถ„์€ controls์— action์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

     


     

    Create an Image Picker to Respond to User Taps

     

    ๊ตฌํ˜„ ๋ชฉํ‘œ

    1. ์‚ฌ์šฉ์ž๊ฐ€ Image view๋ฅผ Tap ํ–ˆ์„ ๋•Œ, ์นด๋ฉ”๋ผ๋กœ ์ƒˆ๋กœ์šด ์‚ฌ์ง„์„ ์ฐ๊ฑฐ๋‚˜, ์•จ๋ฒ”์—์„œ ์‚ฌ์ง„์„ ํ•˜๋‚˜ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค.

    2. ์„ ํƒํ•œ ์‚ฌ์ง„์„ Meal Scene์˜ Image view์—์„œ ๋„์šด๋‹ค.

     

    ์ด๋ฒˆ ๋‹จ๋ฝ์—์„œ๋Š” ์œ„์˜ ๋‘ ๋‹จ๊ณ„๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ํ•ด๊ฒฐํ•˜๋Š” ๊ณผ์ •์„ ๋ฆฌ๋ทฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

     

    1. ์‚ฌ์šฉ์ž๊ฐ€ Image view๋ฅผ Tap ํ–ˆ์„ ๋•Œ, ์นด๋ฉ”๋ผ๋กœ ์ƒˆ๋กœ์šด ์‚ฌ์ง„์„ ์ฐ๊ฑฐ๋‚˜ ์•จ๋ฒ”์—์„œ ์‚ฌ์ง„์„ ํ•˜๋‚˜ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค.

    ์šฐ์„  ์‚ฌ์šฉ์ž์˜ Input (Tap)์„ ์ธ์‹ํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์ด์ „ ๋‹จ๋ฝ์—์„œ Gesture recognizer๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , action์„ ์ƒ์„ฑํ•ด ์—ฐ๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

     

    ์•จ๋ฒ”์—์„œ ์‚ฌ์ง„์„ ํ•˜๋‚˜ ์„ ํƒํ•  ์ˆ˜ ์žˆ๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ UIImagePickerController๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.

    " An image picker controller manages the user interface for taking pictures and for choosing saved images to use in your app."

     

    UIImagePickerController์—์„œ ๊ณ ๋ฅธ ๊ฒฐ๊ณผ๋ฅผ Meal Scene์˜ View controller์—๊ฒŒ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ์ฐธ์กฐ๊ฐ€ ํ•„์š”ํ•œ๋ฐ, ๋งˆ์นจ UIImagePickerController๋Š” delegate ํ”„๋กœํ† ์ฝœ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

     

    ๊ทธ๋ฆฌ๊ณ  ๋ฐ˜๋“œ์‹œ delegate์„ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค๊ณ  ๊ฐ€์ด๋“œ๋ผ์ธ์„ ์ œ์‹œํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

    " To use an image picker controller, you must provide a delegate that conforms to the UIImagePickerControllerDelegate protocol. "

     

    ๋”ฐ๋ผ์„œ delegate ํ”„๋กœํ† ์ฝœ์„ ์ฑ„์šฉํ•˜๋Š” ๊ฐ์ฒด๋Š” View controller๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

     

    ๊ทธ๋ž˜์„œ ์œ„์˜ ์ž‘์—…๋“ค์— ๋”ฐ๋ผ action method์— ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

      @IBAction func selectingImageFromPhotoLibrary(_ sender: UITapGestureRecognizer) {
        
        // Hide the keyboard while typing in the textfield when user taps the image view
        /* 1 */
        nameTextField.resignFirstResponder()
        
        /* 2 */
        let imagePickerController = UIImagePickerController()
        
        /* 3 */
        imagePickerController.sourceType = .photoLibrary
        
        /* 4 */
        imagePickerController.delegate = self
        present(imagePickerController, animated: true, completion: nil)
      }

     

    /* 1 */

    ์‚ฌ์šฉ์ž๊ฐ€ TextField๋ฅผ ์ž‘์„ฑ์ค‘์— image view ๋ฅผ ํƒญํ•˜์˜€์„ ๋•Œ, Text Field ๋ฅผ first responder์—์„œ ํ•ด์ œํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

     

    /* 2*/

    UIImagePickerController ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

    ์ด ์ธ์Šคํ„ด์Šค๋Š” ํƒํ•œ ์‹œ์ ์—๋งŒ ํ•„์š”ํ•˜๋ฏ€๋กœ, ๊ตณ์ด ๋‹ค๋ฅธ ์‹œ์ ์— ์ƒ์„ฑํ•˜์—ฌ ์ฐธ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค.

    ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ  delgate method ํ˜ธ์ถœ ๋ฐ ์‹คํ–‰์ด ์ข…๋ฃŒ๋˜๋ฉด, ARC์— ์˜ํ•ด ์ž๋™์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

     

    /* 3 */

    ์‚ฌ์šฉ์ž์—๊ฒŒ photoLibrary๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

     

    /* 4 */

    delegate๋ฅผ View Controller๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

    ImagePIckerController๋ฅผ ํ™”๋ฉด์— ๋„์šฐ๊ธฐ ์œ„ํ•ด present(_, animated:, completion:) ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.


     

    ๊ทธ๋Ÿฐ๋ฐ View Controller๊ฐ€ delegate ์ด ๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•„์š”ํ•œ ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

     

    ์•„๋ž˜ ์ฝ”๋“œ๋Š” ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

    extension ViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
      
      /* 1 */
      func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated:true, completion: nil)
      }
      
      /* 2 */
      func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        guard let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else {
          fatalError()
        }
        
        photoImageView.image = selectedImage
        
        dismiss(animated: true, completion: nil)
      }
    }
    

     

    /* 1 */

    ์–ด๋–ค ์‹œ์ ์— ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ ์ธ์ง€๋Š” Apple Developer Documentation์„ ํ†ตํ•ด ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    ๋‹ค๋งŒ ๋ฉ”์„œ๋“œ๋ช… ์ค‘ DidCancel์ด๋ผ๋Š” ๋ฌธ๊ตฌ๊ฐ€ ์‚ฌ์šฉ์ž๊ฐ€ `cancel` ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ์งํ›„์— ํ˜ธ์ถœ๋  ๊ฒƒ์„ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

     

    /* 2 */

    ์‚ฌ์šฉ์ž๊ฐ€ ์ด๋ฏธ์ง€๋ฅผ ์„ ํƒํ–ˆ์„ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

    parameter `info`์— ์ •๋ณด๊ฐ€ ๋”•์…”๋„ˆ๋ฆฌ ํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

    ๋ฐฉ์–ด ์ฝ”๋“œ๋กœ. originalImage๋ฅผ key๋กœ ๋„ฃ์–ด ๋‚˜์˜จ value๋ฅผ  UIImage๋กœ ์บ์ŠคํŒ…ํ•˜์—ฌ ์ €์žฅํ•˜๊ณ , ๊ทธ๊ฒƒ์„ ImageView.image ์†์„ฑ์— ๋„ฃ์–ด ์ค๋‹ˆ๋‹ค.


     

    ์ดํ›„ ์‹คํ–‰ํ•ด๋ณด๋ฉด ์ €์˜ ๊ฒฝ์šฐ ์ •์ƒ์ ์œผ๋กœ ๋™์ž‘ํ–ˆ์ง€ ๋งŒ ๋งŒ, Documentaion์—์„œ๋Š” ์ด๋Œ€๋กœ ์‹คํ–‰ํ•˜๋ฉด ์ผ๋ถ€๋Ÿฌ ์—๋Ÿฌ๊ฐ€ ๋œจ๊ฒŒ ์„ค๊ณ„ํ•œ ๊ฒƒ ๊ฐ™์•˜์Šต๋‹ˆ๋‹ค.

     

    ์ด ๋‚ด์šฉ์„ ์ ์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. (์ดํ›„๋Š” Documentation์—์„œ ๋‚˜์˜ค๋Š” ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค.)

    ์ œ๊ฐ€ ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ๋กœ๋งŒ ์‹คํ–‰ํ•ด์„œ ๋ฌธ์ œ์—†์ด ์‹คํ–‰๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

     

    ์‹คํ–‰ํ•˜์—ฌ Image view๋ฅผ ํƒญ ํ•˜๋ฉด, ์•ฑ์ด SIGABORT signal๊ณผ ํ•จ๊ป˜ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.

     

    ์ด๊ฒƒ์€ ์•ฑ์„ ๋ฐ”๋กœ ์ข…๋ฃŒํ•ด์•ผ ํ•  ๋งŒํผ ์‹ฌ๊ฐํ•œ ์—๋Ÿฌ๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

     

    ์ด ๋ฌธ์ œ๋Š” image picker๋ฅผ ํ™”๋ฉด์— present ํ•˜๋ ค๊ณ  ํ•  ๋•Œ ๋ฐœ์ƒํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

     

    ์‹œ์Šคํ…œ์€ ๋ฐ˜๋“œ์‹œ Photo Library์— ์ ‘๊ทผํ•˜๊ธฐ ์ „์— ์‚ฌ์šฉ์ž์—๊ฒŒ ํ—ˆ๊ฐ€๋ฅผ ๋ฐ›์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.

     

    iOS 10 ์ด์ƒ๋ถ€ํ„ฐ, photo library usage description์„ ๋ฐ˜๋“œ์‹œ ์ž‘์„ฑํ•˜์—ฌ์•ผ ํ•˜๊ณ , ์ด๋Ÿฐ ํ—ˆ๊ฐ€ ๋ฌธ๊ตฌ๋Š” Info.plist ์—์„œ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

     

    Info.plist ์—์„œ  ์˜ค๋ฅธ์ชฝ ํด๋ฆญ ํ›„ [Add row] ๋ฅผ ํ†ตํ•ด Property๋ฅผ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•˜๊ณ , ์œ„์™€ ๊ฐ™์ด ์„ค์ • ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

     

     

     

    ๋!


     

    ๋‹ค์Œ ๊ธ€:

    ๋Œ“๊ธ€

์–ด์ œ๋ณด๋‹ค ๋ฐœ์ „ํ•œ ๋‚˜