Bibi's DevLog ๐Ÿค“๐ŸŽ

UITapGestureRecognizer ํƒญ ์ œ์Šค์ฒ˜ ์ธ์‹๊ธฐ ๋ณธ๋ฌธ

๐Ÿ“ฑ๐ŸŽ iOS/๐Ÿ Apple Developer Documentation

UITapGestureRecognizer ํƒญ ์ œ์Šค์ฒ˜ ์ธ์‹๊ธฐ

๋น„๋น„ bibi 2022. 3. 24. 11:44

[iOS, Swift] UITapGestureRecognizer ํƒญ ์ œ์Šค์ฒ˜ ์ธ์‹๊ธฐ

https://cocoacasts.com/swift-fundamentals-working-with-tap-gesture-recognizers-in-swift

UITapGestureRecognizer ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด ํƒญ ์ œ์Šค์ฒ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฒ•.

import UIKit

class ViewController: UIViewController {

    // MARK: - Properties

    @IBOutlet private var tappableView: UIView! {
        didSet {
            tappableView.backgroundColor = .red
        }
    }

    // MARK: - View Life Cycle

    override func viewDidLoad() {
        super.viewDidLoad()
    }

}
  • tappableViewํ•˜๋‚˜๋ฅผ ๋งŒ๋“ค๊ณ  ์‹œ์ž‘
  • Main.storyboard์— ๋ทฐ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ , tappableView์™€ ์—ฐ๊ฒฐ
  • ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ ๋นŒ๋“œ ๋ฐ ์‹คํ–‰ - ํฐ ๋ฐฐ๊ฒฝ์— ๋นจ๊ฐ„ ๋ทฐ๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

๋ทฐ์— tap gesture recognizer ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค - ์ฝ”๋“œ๋กœ ๋˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค ๋นŒ๋”๋กœ. ๊ฒฐ๊ณผ๋Š” ๋˜‘๊ฐ™์ง€๋งŒ ์‹คํ–‰ ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅด๋‹ค.

1. Interface Builder๋กœ ๋ทฐ์— Tap Gesture Recognizer ์ถ”๊ฐ€ํ•˜๊ธฐ

  • Main.storyboard๋ฅผ ์—ด๊ณ  Object Library(+๋ฒ„ํŠผ)์—์„œ tap gesture recognizer๋ฅผ ์•„๊นŒ ๋งŒ๋“  tappableView์— ์ถ”๊ฐ€ํ•œ๋‹ค.
    • ์ •์ƒ์ ์œผ๋กœ ์ถ”๊ฐ€๋˜๋ฉด ์™ผ์ชฝ์˜ Document Outline์— ๋‚˜ํƒ€๋‚œ๋‹ค.
  • ViewController.swift๋ฅผ ์—ด๊ณ  tap gesture recognizer์— ๋Œ€ํ•œ Action์„ ์ •์˜ํ•œ๋‹ค.
    • "Action = Interface Builder์— ๋…ธ์ถœ๋˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งํ•จ"
    • ํ•ด๋‹น Action์€ UITapGestureRecognizer๋ผ๋Š” ํ•˜๋‚˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›๋Š”๋‹ค.
// MARK: - Actions

@IBAction func didTapView(_ sender: UITapGestureRecognizer) {
    print("did tap view", sender)
}
  • ๋‹ค์‹œ Main.storyboard์— ๊ฐ€์„œ ์™ผ์ชฝ Document Outline์—์„œ tap gesture recognizer๋ฅผ ์„ ํƒํ•œ๋‹ค
  • ์˜ค๋ฅธ์ชฝ Connections Inspector๋ฅผ ์—ด๊ณ , Sent Actions์˜ selector์—์„œ Document Outline์˜ View Controller๊นŒ์ง€ ๋“œ๋ž˜๊ทธํ•ด ์—ฐ๊ฒฐํ•œ๋‹ค.
  • didTapView(_:) Action์ด ๋‚˜ํƒ€๋‚˜๋ฉด ํด๋ฆญํ•ด ์—ฐ๊ฒฐํ•œ๋‹ค.
  • ๋‹ค์‹œ ๋นŒ๋“œํ•˜๊ณ  ์‹คํ–‰ํ•œ ๋’ค ๋นจ๊ฐ„ ๋ทฐ๋ฅผ ํด๋ฆญํ•˜๋ฉด, ์ฝ˜์†” ์ฐฝ์— ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.
    • tap gesture recognizer ์—ฐ๊ฒฐ์ด ์ž˜ ๋œ ๊ฒƒ!

์ฝ”๋“œ๋กœ Tap Gesture Recognizer ์ถ”๊ฐ€ํ•˜๊ธฐ

๋จผ์ € ์œ„์—์„œ ์‹ค์Šตํ•œ Interface Builder์˜ tap gesture recognizer๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

  • ViewCotnroller.swift์—์„œ viewDidLoad()๋ฉ”์„œ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค.
  • UITapGestureRecognizer ์ธ์Šคํ„ด์Šค๋ฅผ init(target:action:)๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
    • target ์€ ๋ทฐ์ปจํŠธ๋กค๋Ÿฌ, action์€ ์•„๊นŒ ์ •์˜ํ•œ didTapView(_:)๋กœ ์ •์˜ํ•œ๋‹ค.
// MARK: - View Life Cycle

override func viewDidLoad() {
    super.viewDidLoad()

    // Initialize Tap Gesture Recognizer
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTapView(_:)))
}

// MARK: - Actions

@objc func didTapView(_ sender: UITapGestureRecognizer) {
    print("did tap view", sender)
}
  • โœ… ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•  ๋•Œ๋Š” InterfaceBuilder๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— @IBAction์ด๋ผ๋Š” ์†์„ฑ์ด ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋Œ€์‹  @objc๋ผ๋Š” ์†์„ฑ์ด ํ•„์š”ํ•˜๋‹ค.
    • @objc๋ผ๋Š” ์†์„ฑ์€ ๊ทธ ๋ฉ”์„œ๋“œ๊ฐ€ Objective-C ์˜ ์‹คํ–‰์‹œ๊ฐ„์— ๋…ธ์ถœ๋˜๋„๋ก ํ•œ๋‹ค. ์ž์„ธํ•œ ๊ฑด ๋‚˜์ค‘์— ๋ฐฐ์šฐ๊ฒŒ ๋  ๊ฒƒ..
    • @objc ์†์„ฑ์„ ๋นผ๋จน์œผ๋ฉด ์ปดํŒŒ์ผ๋Ÿฌ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค - Argument of '#selector' refers to instance method 'didTapView' that is not exposed to Objective-C
  • ๋งˆ์ง€๋ง‰์œผ๋กœ tap gesture recognizer๋ฅผ ์ฝ”๋“œ๋กœ ๋ทฐ์— ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. tappableView์— addGuestureRecognizer(_:)๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ tap gesture recognizer๋ฅผ ๋„˜๊ธด๋‹ค.
// MARK: - View Life Cycle

override func viewDidLoad() {
    super.viewDidLoad()

    // Initialize Tap Gesture Recognizer
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTapView(_:)))

    // Add Tap Gesture Recognizer
    tappableView.addGestureRecognizer(tapGestureRecognizer)
}
  • ๋นŒ๋“œ ํ›„ ์‹คํ–‰ํ•ด ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

TapGestureRecognizer๋กœ ๋”๋ธ” ํƒญ ๊ฐ์ง€ํ•˜๊ธฐ

tap gesture recognizer๋Š” ์‹ฑ๊ธ€ ํƒญ, ๋”๋ธ” ํƒญ, ํŠธ๋ฆฌํ”Œ ํƒญ์„ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค. (๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” ์‹ฑ๊ธ€ ํƒญ์„ ๊ฐ์ง€ํ•œ๋‹ค)

์‹ฑ๊ธ€ ํƒญ์ด ์•„๋‹Œ ๋”๋ธ” ํƒญ์„ ๊ฐ์ง€ํ•˜๋„๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • viewDidLoad()์—์„œ tapGestureRecognizer์˜ .numberOfTapsRequired๋ฅผ 2๋กœ ์„ค์ •ํ•œ๋‹ค.
// MARK: - View Life Cycle

override func viewDidLoad() {
    super.viewDidLoad()

    // Initialize Tap Gesture Recognizer
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didTapView(_:)))

    // Configure Tap Gesture Recognizer
    tapGestureRecognizer.numberOfTapsRequired = 2

    // Add Tap Gesture Recognizer
    tappableView.addGestureRecognizer(tapGestureRecognizer)
}
  • ๋นŒ๋“œ ํ›„ ์‹คํ–‰ํ•ด ์‹ฑ๊ธ€ ํƒญ์ด ์•„๋‹Œ ๋”๋ธ” ํƒญ์—๋งŒ didTapView(_:)๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.