从我触摸屏的位置获取坐标 [英] Getting the coordinates from the location I touch the touchscreen

查看:253
本文介绍了从我触摸屏的位置获取坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从触摸屏的位置获取坐标,以在此时放置特定的UIImage。

I try to get the coordinates from the location where I hit the touchscreen to do put a specific UIImage at this point.

我该怎么做?

推荐答案

UIResponder 子类中,例如 UIView

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let touch = touches.anyObject()! as UITouch
    let location = touch.locationInView(self)
}

此将在视图坐标中返回 CGPoint

This will return a CGPoint in view coordinates.

已使用Swift 3语法更新

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let touch = touches.first!
    let location = touch.location(in: self)
}

已使用Swift 4语法更新

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first!
    let location = touch.location(in: self.view)
}

这篇关于从我触摸屏的位置获取坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆