【问题标题】:Value of type 'UITouch' has no member 'locationInNode'“UITouch”类型的值没有成员“locationInNode”
【发布时间】:2016-01-04 10:21:58
【问题描述】:

我正在尝试使用 swift2.1 获取触摸位置并收到错误“'UITouch' 类型的值没有成员 'locationInNode'”。

这是我的代码

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

我查看了源代码,发现在 UITouch 类中只有 public

func locationInView(view: UIView?) -> CGPoint {
}

这是我应该使用的东西还是...?

【问题讨论】:

标签: ios swift2 uitouch touchesbegan


【解决方案1】:

正如 NicolasMiari 在评论中指出的那样,只有当您使用 SpriteKit 时,这才有效。 locationInNode() 是一个 SpriteKit 函数,可帮助您找到 SKNode 中的点。

如果您使用“标准”UIKit 来获取视图中的触摸位置,则应该使用 locationInView() 获取视图(!)而不是控制器本身。

class ViewController : UIViewController{

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多