2013-02-25 61 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 ..