How do I implement the UITapGestureRecognizer into my application

前端 未结 5 1992
梦如初夏
梦如初夏 2020-11-27 06:20

I am quite new to programming and Objective C. I was wondering how to make an app which has a blank screen and a timer that goes for one minute. You are meant to tap as fast

5条回答
  •  难免孤独
    2020-11-27 07:11

    Works in Xcode 11.4, Swift 5

    class ViewController: UIViewController {
    
        @IBOutlet weak var targetView: UIView!
        override func viewDidLoad() {
            super.viewDidLoad()
            let tap = UITapGestureRecognizer(target: self, action: #selector(self.touchCallback(_:)))
            self.targetView.addGestureRecognizer(tap)
        }
    
        @objc func touchCallback(_ sender: UITapGestureRecognizer? = nil) {
            if(sender?.state == .ended) {
                print("tapped")
            }
        }
    }
    
    // If you want to touch start, available touchesBegan
    class ViewController: UIViewController {
    
        @IBOutlet weak var targetView: UIView!
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
    
        override func touchesBegan(_ touches: Set, with event: UIEvent?) {
              for touch in touches {
                  let location = touch.location(in: targetView)
                  print(location)
              }
          }
    }
    

提交回复
热议问题