2013-02-25 66 views
2

我试图创建一个应用程序,允许用户在图像上移动框架,以便我可以在选定区域上应用某些效果。iOS中的接触点坐标没有正确映射

我需要允许用户精确拖动和缩放图像上的遮罩帧。我需要这是确切的,就像任何其他照片应用程序一样。

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

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)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坐标会互换。

请你帮我弄清楚如何得到确切的触摸坐标?

我的背景图像位于一个视图中(imageBG),并且被遮罩的帧位于单独的一个视图中(maskBG)。我已经尝试了:

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

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

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

+0

对这个问题的“Xcode的”标签也是“不正确映射”。 – 2013-02-25 05:55:05

+2

@JoshCaswell *在线高五* – 2013-02-25 05:57:25

+0

@iPatel:[iphone]标签在这里不合适,事实上已经被H2CO3专门去除了。该标签只能用于特定于iPhone硬件的问题 - 设备本身。 – 2013-02-25 06:13:44

回答

0

你好你的问题是图像和iPhone屏幕不一定在相同的长宽比。你的触摸点可能无法正确地转换到您的实际图像。

- (UIImage*) getCroppedImage { 
    CGRect rect = self.movingView.frame; 
    CGPoint a; 
    a.x=rect.origin.x-self.imageView.frame.origin.x; 
    a.y=rect.origin.y-self.imageView.frame.origin.y; 

    a.x=a.x*(self.imageView.image.size.width/self.imageView.frame.size.width); 
    a.y=a.y*(self.imageView.image.size.height/self.imageView.frame.size.height); 

    rect.origin=a; 
    rect.size.width=rect.size.width*(self.imageView.image.size.width/self.imageView.frame.size.width); 
    rect.size.height=rect.size.height*(self.imageView.image.size.height/self.imageView.frame.size.height); 

    UIGraphicsBeginImageContext(rect.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    // translated rectangle for drawing sub image 
    CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, self.imageView.image.size.width, self.imageView.image.size.height); 

    // clip to the bounds of the image context 
    // not strictly necessary as it will get clipped anyway? 
    CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height)); 

    // draw image 
    [self.imageView.image drawInRect:drawRect]; 

    // grab image 
    UIImage* croppedImage = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 

    return croppedImage; 
} 

这是我做的裁剪我运动的看法是,我通过要裁切的矩形看到它如何被转化为对image.Make确保用户在其上看到图像的图像视图正确反映是aspectfit内容模式。

注: - 我让图像视图的矩形适合aspectFit图像

利用这个去做

- (CGSize)makeSize:(CGSize)originalSize fitInSize:(CGSize)boxSize 
{ 
    widthScale = 0; 
    heightScale = 0; 

    widthScale = boxSize.width/originalSize.width; 
    heightScale = boxSize.height/originalSize.height; 

    float scale = MIN(widthScale, heightScale); 

    CGSize newSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale); 

    return newSize; 
} 
+0

这是一个非常有前途的解决方案...等到我尝试了这个! – metsburg 2013-02-25 06:22:42

+0

我添加了一个代码,将您的图像视图设置为方面适合尺寸 – amar 2013-02-25 06:23:44

+0

Didi it work @ user2041826? – amar 2013-02-25 10:27:14

1

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

0

你尝试过这些:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
UITouch *touch = [touches anyObject]; 
CGPoint touchPoint = [touch locationInView:selectedImageView]; 

float currX = (touchPoint.x)/selectedImageView.frame.size.width; 
float currY = (touchPoint.y)/selectedImageView.frame.size.height; 

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

,或者您也可以使用UIPanGestureRecognizer ..