Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get tap point (coordinates) in iOS swift

Tags:

ios

swift

I'm trying to get the coordinates of a touch on the screen, To show a popup menu

how to use in page view controller

What am I doing wrong here?

  override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = touches.first {
      let position = touch.locationInView(view)
      print(position)
    }
  }
like image 653
LeNI Avatar asked Dec 07 '15 09:12

LeNI


1 Answers

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

like image 88
user3182143 Avatar answered Sep 18 '22 14:09

user3182143