将UIImageView触摸坐标转换为UIImage坐标 [英] Converting UIImageView Touch Coordinates to UIImage Coordinates
问题描述
我能够在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
被称为触摸
和 UIImage
被称为图像
。
Assuming the UIImageView
is called imageView
the UITouch
is called touch
and the UIImage
is called image
.
这篇关于将UIImageView触摸坐标转换为UIImage坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!