2011-07-26 45 views
0

我有一個簡單的應用程序,向用戶顯示一個圖像(在UIImageView中),從而他們觸摸屏幕的一部分,然後在下一個屏幕上顯示相同的圖像(在UIImageView中)但現在有一個「簽名」覆蓋在它上面。我遇到的問題是,似乎從觸摸返回的x,y座標似乎無法正確映射到第二個屏幕上的UIImageView。獲取觸摸座標的嚴重問題

例如,當我觸摸屏幕的底部,我得到:X:157.000000 Y:358.000000

但屏幕的底部應該是480?因爲我的屏幕尺寸是:X的屏幕高度和寬度:320.000000 Y:480.000000。這會使簽名放置在不同於用戶想要的位置。

我使用下面的代碼,讓我觸摸座標:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ 

    NSLog(@"TOUCH HAPPENED"); 
    UITouch *touch = [touches anyObject]; 
    CGPoint touchCoordinates = [touch locationInView:self.view]; 
    NSLog(@"X: %f Y: %f",touchCoordinates.x, touchCoordinates.y); 

} 

我使用下面的代碼放置在「簽名」與我從觸摸得到的值:

CGRect screenRect = [[UIScreen mainScreen] bounds]; 
     UIGraphicsBeginImageContext(screenRect.size); 


     [pageImage drawInRect:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height)]; // this is the original image 

     NSLog(@"SCREEN HEIGHT AND WIDTH AT X: %f Y: %f",screenRect.size.width, screenRect.size.height); 


     NSLog(@"PLACING IT AT TOUCH COORDINATES X: %f Y: %f",sigLocation.x, sigLocation.y); 
     UIImage *shieldLogo = [UIImage imageNamed:@"shield_bw.gif"]; 
     [shieldLogo drawInRect:CGRectMake(sigLocation.x, sigLocation.y, shieldLogo.size.width/2, shieldLogo.size.height/2)]; 
     [theSignature drawAtPoint:CGPointMake(sigLocation.x+24.000000, sigLocation.y) withFont:[UIFont fontWithName:@"Arial" size:8.0]]; 
     [theSignature2 drawAtPoint:CGPointMake(sigLocation.x+24.000000, sigLocation.y+ 8.000000) withFont:[UIFont fontWithName:@"Arial" size:8.0]]; 

     UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     [image setImage:resultingImage]; 

回答

3

這個問題可能與用戶觸摸的觀點有關。您可以嘗試(如this link看到)

UITouch * touch = [touches anyObject]; 
CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow]; 
NSLog(@"Position of touch: %.3f, %.3f", pos.x, pos.y); 

這應該給觸摸相對於屏幕的位置。

+0

非常感謝!這讓我浪費了幾個小時。有趣的我總是認爲屏幕和視圖可以互換。 – jini