无论分辨率

时间:2016-11-20 00:39:51

标签: ios swift cocoa-touch swift3

你能给我一个方法来获得所有iOS设备中相同的坐标吗?

我在文章Coordinate Systems and Transforms

中找到了
  

Cocoa中的用户坐标空间是您用于所有绘图命令的环境。它表示一个固定比例的坐标空间,这意味着您在此空间中发出的绘图命令会产生尺寸一致的图形,而不管底层设备的分辨率如何。

我试着这样做:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
    if let touch = touches.first{
        let lastPoint = touch.location(in: self.view.superview)
        let precisePoint = graphView.convert(lastPoint, to:    self.view.superview)
    }
}

但是lastPoint和exactPoint依赖于使用过的设备。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

感谢@Rikh的回答:

var points = [CGPoint]()

//MARK: Touch events
override func touchesBegan(_ touches: Set<UITouch>,
                           with event: UIEvent?){
    if let touch = touches.first{
        let lastPoint = touch.location(in: self.view.superview)

        let bounds = UIScreen.main.bounds

        points.append(CGPoint(x: lastPoint.x / bounds.maxX, y: lastPoint.y / bounds.maxY))
    }
}


func convertPointsToView(){
    let bounds = UIScreen.main.bounds

    for point in points{
        let convertedPoint = CGPoint(x: point.x * bounds.maxX, y: point.y * bounds.maxY)

        print(convertedPoint)
    }
}