how to get tap point (coordinates) in iOS swift

Swift 5:

override func viewDidLoad() {
    super.viewDidLoad()
    
    let tap = UITapGestureRecognizer(target: self, action: #selector(touchedScreen(touch:)))
    view.addGestureRecognizer(tap)
}

@objc func touchedScreen(touch: UITapGestureRecognizer) {
    let touchPoint = touch.location(in: self.view)
    print(touchPoint)
}

In PageViewController,if you want to get pop up

In viewDidLoad:

let tap = UITapGestureRecognizer(target: self, action: "showMoreActions:")
    tap.numberOfTapsRequired = 1
    view.addGestureRecognizer(tap)

Make the page view controller inherit UIGestureRecognizerDelegate then add:

  func showMoreActions(touch: UITapGestureRecognizer) {

        let touchPoint = touch.locationInView(self.view)
        let DynamicView = UIView(frame: CGRectMake(touchPoint.x, touchPoint.y, 100, 100))
        DynamicView.backgroundColor=UIColor.greenColor()
        DynamicView.layer.cornerRadius=25
        DynamicView.layer.borderWidth=2
        self.view.addSubview(DynamicView)

}

UIPageViewController with TouchEvent

Tags:

Ios

Swift