获取触摸坐标时出现严重问题

发布于 2024-11-26 11:44:41 字数 1762 浏览 0 评论 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];

I have a simple application that shows the user an image (in a UIImageView) whereby they touch on a part of the screen and then on the next screen the same image is shown (in a UIImageView) but now has a "signature" overlayed on it. The issue I am having is that it seems that the x,y coordinates returned from the touches do not seem to map properly on the UIImageView on the second screen.

For instance when I touch the bottom of the screen I get: X: 157.000000 Y: 358.000000

but the bottom of the screen should be 480 ? since my screen dimensions are: SCREEN HEIGHT AND WIDTH AT X: 320.000000 Y: 480.000000. This causes the signature to be placed at a DIFFERENT spot than what the user intended.

I use the following code to get my touch coordinates:

-(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);

}

I am using the following code to place the "signature" with the values I get from the touch:

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独享拥抱 2024-12-03 11:44:41

问题可能在于用户正在触摸的视图。您可以尝试(如 此链接

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

这应该给出触摸相对于屏幕的位置。

The issue might be with the view that the user is touching. You might try (as seen in this link)

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

This should give the location of the touch relative to the screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文