Gesture Recognizer

Gesture Recognizer

스토리보드를 통한 연결

@IBAction func handlePan(recognizer:UIPanGestureRecognizer) {
  let translation = recognizer.translation(in: self.view)
  if let view = recognizer.view {
    view.center = CGPoint(x:view.center.x + translation.x,
                            y:view.center.y + translation.y)
  }
  recognizer.setTranslation(CGPoint.zero, in: self.view)
}

샘플코드

import UIKit
class GestureVC: UIViewController, UIGestureRecognizerDelegate {

    @IBOutlet var topLabel:UILabel!
    @IBOutlet var secondLabel:UILabel!
    
    var count:String = ""
    var location:String = ""
    
    @IBAction func tapGesture(_ sender: UITouch) {
        //topLabel.text = count
        //secondLabel.text = location
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        count = "Tap Count : \(touch.tapCount) "
        location = "X 좌표: \(touch.location(in: self.view).x) " + "Y 좌표: \(touch.location(in: self.view).y)"
        topLabel.text = count
        secondLabel.text = location
        return true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

}