为什么我的触摸坐标在屏幕上没有正确映射?

时间:2011-07-26 08:00:26

标签: iphone objective-c ios

我在屏幕上显示UIImage,用户可以点按并放置签名。一旦点击发生,另一个UIImage加载相同的图像并放置签名。然而,我所看到的是,用户点击的位置不一定是签名的位置。

这是我的代码:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint touchCoordinates = [touch locationInView:self.view];
    NSLog(@"X: %f   Y: %f",touchCoordinates.x, touchCoordinates.y);

    SignDocumentController *signDocumentController = [[SignDocumentController alloc]initWithNibName:@"SignDocumentController" bundle:nil];
    signDocumentController.navigationItem.title=@"Sign Document";
    signDocumentController.sigLocation = touchCoordinates;

    [self.navigationController pushViewController:signDocumentController animated:YES];
    [signDocumentController release];

}

//lets now place the image
CGPDFPageRef page = CGPDFDocumentGetPage(document, 1);
            UIImage *pageImage = [PDFPageConverter convertPDFPageToImage:page withResolution:144];
UIImage *shieldLogo = [UIImage imageNamed:@"shield_bw.gif"];
        UIGraphicsBeginImageContext(pageImage.size);
        [pageImage drawInRect:CGRectMake(0, 0, pageImage.size.width, pageImage.size.height)];


        NSLog(@"PLACING IT AT   X: %f   Y: %f",sigLocation.x, sigLocation.y);

        [shieldLogo drawInRect:CGRectMake(sigLocation.x, sigLocation.y, shieldLogo.size.width, shieldLogo.size.height)];
        [theSignature drawAtPoint:CGPointMake(sigLocation.x+50, sigLocation.y) withFont:[UIFont fontWithName:@"Arial" size:15.0]];
        [theSignature2 drawAtPoint:CGPointMake(sigLocation.x+50, sigLocation.y+ 20) withFont:[UIFont fontWithName:@"Arial" size:15.0]];



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

为SignDocumentController添加类定义

#import <UIKit/UIKit.h>


@interface SignDocumentController : UIViewController<NSXMLParserDelegate> {
    NSMutableString *signFaxString;
    NSString * messageId;
    NSMutableData *xmlData;
    NSURLConnection *connectionInprogress;
    NSURLConnection *connectionInprogress2;

    NSString * annotationKey;
    NSString *firstName;
    NSString *lastName;
    NSString *date;
    NSString *signature;

    CGPoint sigLocation;

    UIActivityIndicatorView *activityIndicator;

    IBOutlet UIImageView *image;
}

@property(nonatomic,copy)NSString * firstName;
@property(nonatomic,copy)NSString * lastName;
@property(nonatomic,copy)NSString * date;
@property(nonatomic,copy)NSString * signature;
@property(nonatomic,copy)NSString * annotationKey;
@property(nonatomic,retain)UIImageView * image;
@property(assign)CGPoint sigLocation;

-(IBAction) btnNext;


@end

1 个答案:

答案 0 :(得分:1)

问题是否与您正在阅读相对于给定视图的触摸坐标这一事实有关,但是您在另一个视图中使用它们?在这种情况下,两个视图之间的相对偏移可能产生你看到的差异......你应该使用相对于你正在绘制的UIImage的坐标......

试试这个:

CGPoint touchCoordinates = [touch locationInView:self.imageView];

其中self.imageView包含您要重绘的UIImage ...

老答案:

我注意到你在某些时候会这样做:

signDocumentController.sigLocation = touchCoordinates;
然后,当您绘制图像时,使用sigLocation,这应该是一些ivar变量。

    [shieldLogo drawInRect:CGRectMake(sigLocation.x, sigLocation.y, shieldLogo.size.width, shieldLogo.size.height)];
    [theSignature drawAtPoint:CGPointMake(sigLocation.x+50, sigLocation.y) withFont:[UIFont fontWithName:@"Arial" size:15.0]];
    [theSignature2 drawAtPoint:CGPointMake(sigLocation.x+50, sigLocation.y+ 20) withFont:[UIFont fontWithName:@"Arial" size:15.0]];

我找不到sigLocation的任何其他引用,因此此变量应包含一些与当前位置无关的值。这可能是触摸和图像位置不匹配的原因吗?