OGeek|极客世界-中国程序员成长平台

标题: ios - iOS 中的接触点坐标未正确映射 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:06
标题: ios - iOS 中的接触点坐标未正确映射

我正在尝试创建一个允许用户在图像上移动框架的应用程序,以便我可以在选定区域上应用一些效果。

我需要允许用户精确地拖动和缩放图像上的蒙版框。我需要准确地说,就像任何其他照片应用一样。

我的策略是在触摸移动事件上获取用户的触摸点,并相应地缩放我的框架。那很直观。我编写了以下代码来处理触摸移动事件:

-(void)touchesMovedNSSet *)touches withEventUIEvent *)event
{

    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:[self view]];


    float currX = touchPoint.x;
    float currY = touchPoint.y;

    /*proceed with other operations on currX and currY, 
      which is coming out quite well*/

}

但唯一的问题是,currXcurrY 变量的坐标并不是它们应该在的位置。存在视差错误,不断从设备转移到设备。我还认为在 iPad 的情况下 x 和 y 坐标会被交换。

您能帮我弄清楚如何获得准确的触摸坐标吗?

我的背景图像在一个 View 中 (imageBG),而蒙版帧在一个单独的 View 中 (maskBG)。我试过了:

CGPoint touchPoint = [touch locationInView:[maskBG view]];

CGPoint touchPoint = [touch locationInView:[imageBG view]];

...但同样的问题仍然存在。我还注意到 iPad 上的触摸错误比 iPhone 或 iPod 上更严重。



Best Answer-推荐答案


image.center = [[[event allTouches] anyObject] locationInView:self.view];

关于ios - iOS 中的接触点坐标未正确映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15060902/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4