Iphone 接触坐标的严重问题
我有一个简单的应用程序,它向用户显示一个图像(在UIImageView中),用户通过触摸屏幕的一部分,然后在下一个屏幕上显示相同的图像(在UIImageView中),但现在它上面覆盖了一个“签名”。我遇到的问题是,触摸返回的x,y坐标似乎没有正确映射到第二个屏幕上的UIImageView上 例如,当我触摸屏幕底部时,我得到:X:157.000000 Y:358.000000 但是屏幕的底部应该是480?因为我的屏幕尺寸是:X:320.000000的屏幕高度和宽度Y:480.000000。这会导致签名放置在不同于用户预期的位置 我使用以下代码获取触摸坐标:Iphone 接触坐标的严重问题,iphone,ios,uiimageview,Iphone,Ios,Uiimageview,我有一个简单的应用程序,它向用户显示一个图像(在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];
问题可能在于用户正在触摸的视图。您可以尝试(如中所示)
这应该给出触摸相对于屏幕的位置。问题可能在于用户正在触摸的视图。您可以尝试(如中所示)
这将给出触摸屏相对于屏幕的位置。非常感谢!!!!这让我浪费了很多时间。有趣的是,我一直认为屏幕和视图可以互换。非常感谢!!!!这让我浪费了很多时间。有趣的是,我一直认为屏幕和视图可以互换。
UITouch * touch = [touches anyObject];
CGPoint pos = [touch locationInView: [UIApplication sharedApplication].keyWindow];
NSLog(@"Position of touch: %.3f, %.3f", pos.x, pos.y);