【问题标题】:how to get tap point (coordinates) in iOS swift如何在iOS swift中获取点击点(坐标)
【发布时间】:2015-12-07 09:18:21
【问题描述】:

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

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

我在这里做错了什么?

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

【问题讨论】:

  • 这段代码运行良好,我在模拟器中用 iOS 9.1 sdk 测试过
  • 但这对我不起作用,它有任何 deleget 或其他工作代码
  • 我现在尝试了你的编码。它对我来说很好。你使用的是哪个 xcode?​​span>
  • 还有你用的是哪个iOS版本?

标签: ios swift


【解决方案1】:

在PageViewController中,如果你想弹出

在 viewDidLoad 中:

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

让页面视图控制器继承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)

}

UIPageViewController with TouchEvent

【讨论】:

  • 究竟有什么区别?
  • 我在我的 xcode 中应用了上述编码,它对我来说很好。
  • 我设置了断点,每次触摸视图时它都会调用。
  • 在页面视图控制器中如何实现这段代码
  • 添加到 viewDidLoad :: 不能将视图控制器类型的值分配给“UIGesterRecognizerDelegate”类型的值?
【解决方案2】:

斯威夫特 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)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    相关资源
    最近更新 更多