如何在iOS swift中获取分接点(坐标)

LeN*_*eNI 10 ios swift

我正试图在屏幕上获取触摸的坐标,以显示弹出菜单

如何在页面视图控制器中使用

我在这做错了什么?

  override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = touches.first {
      let position = touch.locationInView(view)
      print(position)
    }
  }
Run Code Online (Sandbox Code Playgroud)

use*_*143 14

在PageViewController中,如果要弹出窗口

在viewDidLoad中:

let tap = UITapGestureRecognizer(target: self, action: "showMoreActions:")
    tap.numberOfTapsRequired = 1
    view.addGestureRecognizer(tap)
Run Code Online (Sandbox Code Playgroud)

使页面视图控制器继承UIGestureRecognizerDelegate然后添加:

  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)

}
Run Code Online (Sandbox Code Playgroud)

带TouchEvent的UIPageViewController


Exe*_*thm 5

斯威夫特 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)
}
Run Code Online (Sandbox Code Playgroud)