gpt4 book ai didi

ios - 在 Swift 1.2 中获取触摸坐标

转载 作者:可可西里 更新时间:2023-11-01 01:06:02 24 4
gpt4 key购买 nike

我试图获取屏幕上触摸的坐标,但每次它只给我 0、0。我只能在新版本的 Swift 中找到有关此功能的基本帮助,而没有任何关于这个问题的信息.我在这里做错了什么?

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
let touch = touches.first as! UITouch
let location = touch.locationInView(self.view)
NSLog("location: %d, %d", location.x, location.y)
}

谢谢!

最佳答案

根据 swift 1.2

override func touchesEnded(touches: Set<NSObject>!, withEvent event: UIEvent!) {
if let touch = touches.first as? UITouch {
let location = touch.locationInView(self.view)
println("Location: \(location)")
}
}

根据 swift 1.1

override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
let touch = touches.anyObject() as? UITouch
let location = touch?.locationInView(self.view)
println("Location: \(location)")
}

已测试。

关于ios - 在 Swift 1.2 中获取触摸坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29977830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com