gpt4 book ai didi

iphone - 为什么我的触摸坐标无法在屏幕上正确映射?

转载 作者:行者123 更新时间:2023-11-29 05:01:23 25 4
gpt4 key购买 nike

我正在屏幕上显示 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

最佳答案

问题是否与以下事实有关:您正在读取相对于给定 View 的触摸坐标,但随后您在另一个 View 中使用它们?在这种情况下,两个 View 之间的相对偏移可能会产生您所看到的差异...您绝对应该使用相对于您正在绘制的 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的引用,因此这个变量应该包含一些与当前位置没有太大关系的值。这可能是触摸和图像位置不匹配的原因吗?

关于iphone - 为什么我的触摸坐标无法在屏幕上正确映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6826989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com