【问题标题】:Serious issues with getting touch coordinates获取触摸坐标的严重问题
【发布时间】:2011-07-26 20:00:10
【问题描述】:

我有一个简单的应用程序,它向用户显示图像(在 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];

【问题讨论】:

    标签: iphone ios uiimageview


    【解决方案1】:

    问题可能与用户正在触摸的视图有关。你可以试试(见this link

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

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

    【讨论】:

    • 非常感谢!!!!这让我浪费了几个小时。有趣的是,我一直认为屏幕和视图可以互换。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-28
    • 2011-02-25
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多