开发者

why are my touch coordinates not mapping properly on screen?

I am displaying a UIImage on screen where the user can tap and place a signature. Once the tap happens, another UIImage is loaded with the same image and a signature is placed. However what I am seeing is that where the user clicks is not necessarily where the signature is placed.

Here is my code:

-(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";
    signDocument开发者_JAVA百科Controller.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];

Adding class definition for 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


Could the problem be related to the fact that you are reading touch coordinates relative to a given view, but then you are using them in another view? In such case, the relative offset between the two views could produce the difference you see... you should definitely use coordinates relative to the UIImage you are drawing...

Try this:

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

where self.imageView contains the UIImage you would like to redraw...

OLD ANSWER:

I notice that at some point you do:

signDocumentController.sigLocation = touchCoordinates;

bu then, when you draw the image you use sigLocation which should be some ivar variable.

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

I cannot find any other reference to sigLocation, so this variable should contain some value that has not much to do with the current location. Could be this the cause of the mismatch between touch and image location?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜