手指跟踪与屏幕IOS上的触摸不匹配
我是stack overflow的新成员,但多年来我一直在使用这些论坛中找到的答案。我有一个问题,如果我对这个主题进行更深入的研究,可能会得到更好的回答,但我正在为我在学校的一个班级进行协作,并已被委托对其他人的代码进行一些更改。。。坦率地说,我的时间不多了,所以我希望能在这里得到一些帮助 我已经研究了这里找到的其他一些问题和答案,但没有一个能直接回答我的问题 我有一个应用程序,应该捕获一个人的签名。它目前在iPhone4上运行得很好,当我试图改变硬件时,问题就来了。如果我选择一个3.5英寸的屏幕或任何版本的ipad,我用签名进行的触摸在屏幕顶部都是准确的,但随着我向下移动,触摸的准确度越来越低(就好像所描绘的图像在一个4英寸的屏幕上,而触摸发生在一个3.5英寸的屏幕上) 以下是我的ViewController代码:手指跟踪与屏幕IOS上的触摸不匹配,ios,touchesmoved,Ios,Touchesmoved,我是stack overflow的新成员,但多年来我一直在使用这些论坛中找到的答案。我有一个问题,如果我对这个主题进行更深入的研究,可能会得到更好的回答,但我正在为我在学校的一个班级进行协作,并已被委托对其他人的代码进行一些更改。。。坦率地说,我的时间不多了,所以我希望能在这里得到一些帮助 我已经研究了这里找到的其他一些问题和答案,但没有一个能直接回答我的问题 我有一个应用程序,应该捕获一个人的签名。它目前在iPhone4上运行得很好,当我试图改变硬件时,问题就来了。如果我选择一个3.5英寸的屏
@implementation SignatureViewController
@synthesize clearButton;
@synthesize tempDrawImage;
@synthesize tempSaveImage;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
red = 0.0/255.0;
green = 0.0/255.0;
blue = 0.0/255.0;
brush = 5.0;
opacity = 1.0;
[self.tempDrawImage setImage:[UIImage imageNamed:@"bg-signature-portrait.png"]];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(self.view.frame.size);
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
[self.tempDrawImage setAlpha:opacity];
UIGraphicsEndImageContext();
lastPoint = currentPoint;
HousingAppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];
appDelegate.tempSig = self.tempDrawImage.image;
}
- (IBAction)clearSig:(id)sender {
[self.tempDrawImage setImage:[UIImage imageNamed:@"bg-signature-portrait.png"]];
return;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(!mouseSwiped) {
UIGraphicsBeginImageContext(self.view.frame.size);
[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
- (void)viewDidDisappear:(BOOL)animated {
[self.tempDrawImage setImage:[UIImage imageNamed:@"bg-signature-portrait.png"]];
}
@end
我假设touchsmoved方法中的“self.view.frame.size.width”和“self.view.frame.size.height”会提取有关硬件屏幕大小的信息并动态填充,但显然不是这样
任何帮助都将不胜感激
此外,当前签名被转置保存在bg-signature-portriat.png背景图像的顶部(一条简单的线,上面有一个x)。我想单独获得签名,但无法理解如何做到这一点(也许这需要UIImage的分层?如果不访问故事板,就无法轻松解释一些事情?)
提前非常感谢您,我保证今后会有更好的问题(希望有一些答案)。试试这个,它可能会帮助您,并且很容易更改代码
- (void)showPopOverscreenforSignatureScreen //:(UIButton *)sender call this method in button action
{
mouseMoved = 0;
[SignatureContentView setHidden:NO];
SignatureContentView.frame=CGRectMake(kLabelWidth+20, 150, 500, 200);
signaturedImageView =(UIImageView *)[self.view viewWithTag:imageViewSelectedTagvalue];
[signaturedImageView setHidden:YES];
}
- (void)allocPopOverscreenforSignatureScreen
{
SignatureContentView =[[UIView alloc]initWithFrame:CGRectMake(0, 50, 500, 200)];
SignatureContentView.layer.cornerRadius=5.0f;
SignatureContentView.layer.backgroundColor=[UIColor lightGrayColor].CGColor;
SignatureContentView.backgroundColor=[UIColor grayColor];
[self.view addSubview:SignatureContentView];
signatureImageView = [[UIImageView alloc] initWithImage:nil];
signatureImageView.layer.cornerRadius=5.0f;
signatureImageView.layer.backgroundColor=[UIColor lightGrayColor].CGColor;
signatureImageView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"textured_paper_.png"]];
signatureImageView.frame = CGRectMake(0, 0, 500, 148);
signatureImageView.userInteractionEnabled=YES;
[SignatureContentView addSubview:signatureImageView];
mouseMoved = 0;
UIButton *cancelButton =[UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.frame=CGRectMake(50, 150, 100, 40);
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
cancelButton.backgroundColor=[UIColor colorWithRed:0.2 green:0.6 blue:.9 alpha:1.5];
[cancelButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[cancelButton addTarget:self action:@selector(cancelsignatureImageView) forControlEvents:UIControlEventTouchUpInside];
[SignatureContentView addSubview:cancelButton];
UIButton *clearButton =[UIButton buttonWithType:UIButtonTypeCustom];
clearButton.frame=CGRectMake(200, 150, 100, 40);
[clearButton setTitle:@"Clear" forState:UIControlStateNormal];
clearButton.backgroundColor=[UIColor colorWithRed:0.2 green:0.6 blue:.9 alpha:1.5];
[clearButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[clearButton addTarget:self action:@selector(clearsignatureImageView) forControlEvents:UIControlEventTouchUpInside];
[SignatureContentView addSubview:clearButton];
UIButton *useButton =[UIButton buttonWithType:UIButtonTypeCustom];
useButton.frame=CGRectMake(350, 150, 100, 40);
useButton.backgroundColor=[UIColor colorWithRed:0.2 green:0.6 blue:.9 alpha:1.5];
[useButton setTitle:@"Use" forState:UIControlStateNormal];
[useButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[useButton addTarget:self action:@selector(getSignaturefromsignatureImageView) forControlEvents:UIControlEventTouchUpInside];
[SignatureContentView addSubview:useButton];
[SignatureContentView setHidden:YES];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
mouseSwiped = NO;
//The below statements will help to get the exact point user touches with out this it will take ZERO yaxis
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:signatureImageView];
lastPoint.y -= 20;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:signatureImageView];
currentPoint.y -= 20;
UIGraphicsBeginImageContext(signatureImageView.frame.size);
[signatureImageView.image drawInRect:CGRectMake(0, 0, signatureImageView.frame.size.width, signatureImageView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapButt);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
signatureImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 20) {
mouseMoved = 0;
}
}
- (void)clearsignatureImageView
{
signatureImageView.image = nil;
}
- (void)cancelsignatureImageView
{
[SignatureContentView setHidden:YES];
[signaturedImageView setHidden:NO];
}
- (void)getSignaturefromsignatureImageView
{
signaturedImageView.image=nil;
if (signatureImageView.image !=nil) {
AppDelegate *delegate =[AppDelegate sharedInstanceofAppdelegate];
UIGraphicsBeginImageContext(signatureImageView.frame.size);
[signatureImageView.image drawInRect:CGRectMake(0, 0, signatureImageView.frame.size.width, signatureImageView.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.0, 0.0, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
signatureImageView.image = UIGraphicsGetImageFromCurrentImageContext();
// UIImageWriteToSavedPhotosAlbum(signatureImageView.image, nil, nil, nil);// black
UIGraphicsEndImageContext();
[SignatureContentView setHidden:YES];
[signaturedImageView setHidden:NO];
NSArray *docArr=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath =[NSString stringWithFormat:@"%@/%@",[docArr objectAtIndex:0],[NSString stringWithFormat:@"signature%@.png",[self PathNameforDocuments]]];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
filePath =[[docArr objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"signature%@.png",[self PathNameforDocuments]]];
NSData* pictureData = UIImagePNGRepresentation(signatureImageView.image);
BOOL success= [pictureData writeToFile:filePath atomically:YES ];
if (success) {
delegate.userSignatureImagePathString=filePath;
}
delegate.userSignatureImage=signatureImageView.image;
signaturedImageView =(UIImageView *)[self.view viewWithTag:imageViewSelectedTagvalue];
signaturedImageView.image=delegate.userSignatureImage;
}
else
{
// please sign
}
}
-(void)viewDidLoad
{
[self allocPopOverscreenforSignatureScreen];
}
使用UIgestureRecognitizerDelegate
委托并全局声明这些对象:
UIImageView *signatureImageView, *signaturedImageView;
BOOL mouseSwiped;
int mouseMoved;
UIView * SignatureContentView;
在我的一个应用程序中,我使用了签名功能,它是在带有图像视图(用于签名)和按钮的视图上创建的,用于“清除”和“取消”。如果你对这种代码格式没意见,我会发给你的。那太好了。我过去使用模板代码时遇到的问题是,很难将其解决到现有的故事板上(这显然只是我理解上的差距)。但你所能做的一切都会有所帮助。谢谢,请检查我的答案,最好将此作为示例code@krayzk这个答案有用吗?是的,非常有用,非常感谢。虽然我最终使用了通过cocoapods导入的jharwigs PPSSignatureview,但在这里可以找到:因为它考虑了许多我没有考虑过的其他部件,可以自由使用。对于其他与类似问题斗争的人,我强烈推荐这条路线。