2014-03-05 51 views
-1

我是堆棧溢出的新成員,但我一直在使用這些論壇中找到的答案多年。我有一個問題可以在我更加深入地研究這個話題的時候得到更好的回答,但是我正在爲我在學校的一個班級進行合作,並且已經委託對別人的代碼進行一些修改......並坦率地說準時不足,所以我希望能在這裏得到一些幫助。手指跟蹤不符合屏幕觸摸IOS

我研究了一些其他問題和答案,但沒有一個人似乎直接解決了我的問題。

我有一個應用程序應該捕獲一個人的簽名。它目前正在爲iphone 4工作,當我嘗試更換硬件時,問題就來了。如果我選擇3.5英寸的屏幕或任何版本的ipad,我用我的簽名製作的觸摸在屏幕的頂部是準確的,但隨着我向下移動,逐漸變得越來越不準確(就好像圖像被描繪出來一樣是4英寸的屏幕,而觸摸正在發生一個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 

我將認爲「self.view.frame.size .width「和」self.view.frame.size.height「在touchesMoved方法將拉動關於硬件的屏幕大小的信息並動態地填充它,但顯然情況並非如此。

任何幫助將不勝感激。

另外,當前簽名被保存在bg-signature-portriat.png背景圖像(帶有x的簡單線條)上。我想單獨獲取簽名,但無法理解如何做到這一點(也許這需要對UIImage進行分層?並且在沒有訪問故事板的情況下無法輕易解釋這些內容?)。

非常感謝你提前,我保證有更好的問題(希望有一些答案)前進。

+0

我使用簽名功能的地方在帶有圖像視圖(用於簽名)的視圖和用於使用,清除,取消的按鈕上創建。如果你對這種代碼格式沒問題,我會寄給你 –

+0

這很好,過去使用模板代碼時遇到的問題是很難將它解決到現有的故事板(這顯然只是一種理解我的差距)。但是,任何你可能會有幫助。謝謝 – krayzk

+0

請檢查我的答案更好試試這個作爲示例代碼 –

回答

1

試一試它可以幫助你輕鬆更改代碼

- (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]; 
} 

使用UIGestureRecognizerDelegate代表和全球聲明這些對象:在我的應用程序之一

UIImageView *signatureImageView, *signaturedImageView; 
BOOL mouseSwiped; 
int mouseMoved; 
UIView * SignatureContentView; 
+0

@krayzk這個答案有幫助 –

+1

是的,這是非常有益的,非常感謝。雖然我最終使用通過cocoapods導入的jharwigs PPSSignatureview: https://github.com/jharwig/PPSSignatureView 因爲它考慮了許多其他部分,我沒有考慮過並且可以自由使用。對於任何其他與類似問題鬥爭的人,我會強烈推薦這條路線。 – krayzk

相關問題