在Swift 1.2中获取触摸坐标

时间:2015-04-30 21:13:00

标签: ios swift uitouch cgpoint

我正试图在屏幕上获得触摸的坐标,但每次它只给我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)
}

谢谢!

1 个答案:

答案 0 :(得分:3)

根据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)")
    }

测试。