将 UIImageView 触摸坐标转换为 UIImage 坐标 [英] Converting UIImageView Touch Coordinates to UIImage Coordinates

查看:21
本文介绍了将 UIImageView 触摸坐标转换为 UIImage 坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够确定 UIImageView 上的触摸事件的坐标.然后我需要将这些坐标转换为正在显示的图像的坐标 - 这应该是不同的,因为我的图像比 UIImageView 大得多,并且我正在使用 AspectFit 缩放图像.

I'm able to determine the coordinates of a touch event on a UIImageView. I need to then convert these coordinates to the coordinates of the image being displayed - which should be different, since my image is much larger than the UIImageView and I'm scaling the image with AspectFit.

在给定 UIImageView 的视图坐标的情况下,确定触摸事件的图像坐标的最佳方法是什么?

What's the best way to determine the image coordinates of a touch event given the view coordinates of a UIImageView?

推荐答案

这样的事情应该能让你继续前进.

Something like this should get you going.

CGPoint imageViewPoint = [touch locationInView:imageView];

float percentX = imageViewPoint.x / imageView.frame.size.width;
float percentY = imageViewPoint.y / imageView.frame.size.height;

CGPoint imagePoint = CGPointMake(image.size.width * percentX, image.size.height * percentY);

假设 UIImageView 被称为 imageView UITouch 被称为 touchUIImage 被称为 image.

Assuming the UIImageView is called imageView the UITouch is called touch and the UIImage is called image.

这篇关于将 UIImageView 触摸坐标转换为 UIImage 坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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