-1

在触摸图像时,我想获取该像素的坐标,我尝试了一些代码,但没有获得用户触摸图像的确切位置。

这是我现有的代码

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


        let Z1 = self.image!.size.height

        let Z2 = self.image!.size.width

        let Z3 = self.bounds.minY

        let Z4 = self.bounds.minX

        let Z5 = self.bounds.height

        let Z6 = self.bounds.width

        let pos1 = (position.x - Z4) * Z2 / Z6

        let pos2 = (position.y - Z3) * Z1 / Z5

        let point = CGPoint(x: pos1, y: pos2)
    }
}
4

1 回答 1

1

你的 touchesBegan 被调用了吗?如果是这样,这应该足够了..

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let pos = touch.location(in: imageView)
            print(pos.x)
            print(pos.y)
        }
    }
于 2019-08-22T07:22:13.710 回答