2012-02-28 106 views
-1

我需要通過使用默認委託方法觸摸開始和觸動移動來處理繪圖應用程序。如何繪製精確的x和y位置的線條?

但是當我在屏幕上的任何一點劃一條線時,它們的x和y位置會自動改變。

在我的應用程序中的實際問題是:我在一個位置繪製一條線,但該線顯示在另一個位置。我需要做些什麼來解決這個問題?

我的代碼是:提前

drawImage = [[UIImageView alloc] initWithImage:nil]; 
///drawImage.frame=CGRectMake(0,0, 320, 380); 
drawImage.frame = self.view.frame; 
[self.view addSubview:drawImage]; 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSLog(@"touches moved method"); 
    mouseSwiped = YES; 

    UITouch *touch = [touches anyObject];  
    CGPoint currentPoint = [touch locationInView:self.view]; 
    currentPoint.y -= 20; 

    UIGraphicsBeginImageContext(self.view.frame.size); 
    [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    ///[drawImage.image drawInRect:CGRectMake(0, 0, 320, 380)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 

    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), components[0],components[1],components[2],alpha); 

    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    lastPoint = currentPoint; 
    ///mouseMoved++; 

    mouseMoved++; 

    if (mouseMoved == 10) 
    { 
     NSLog(@"if mouse moved is equal to 10"); 
     mouseMoved = 0; 
    } 
    NSLog(@"touches moved method ended"); 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    NSLog(@"touches Ended method starting"); 
    UITouch *touch = [touches anyObject]; 

    if ([touch tapCount] == 2) 
    { 
     NSLog(@"touches tap count==2"); 
     ///drawImage.image = nil; 
     return; 
    }  

    if(!mouseSwiped) 
    { 
     NSLog(@"not equla mouse swiped"); 
     UIGraphicsBeginImageContext(self.view.frame.size); 
     [drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
     ///[drawImage.image drawInRect:CGRectMake(0, 0, 320, 380)]; 
     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
     if (tagindex==1) 
     { 
      NSLog(@"1111"); 
      CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 2.0); 
     } 
     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 
     CGContextStrokePath(UIGraphicsGetCurrentContext()); 
     CGContextFlush(UIGraphicsGetCurrentContext()); 
     drawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
    } 
} 

感謝

+1

得到了與您認爲問題出在哪裏相關的任何代碼? – Joel 2012-02-28 12:49:02

+2

你能展示一幅能幫助我們瞭解正在發生的事情的圖像嗎?或者你正在使用的代碼來繪製? – sosborn 2012-02-28 12:49:30

+1

你能請出示一部分代碼嗎? – HarshIT 2012-02-28 12:51:19

回答

1

嘗試替代

UIGraphicsBeginImageContext(self.view.frame.size); 
[drawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

UIGraphicsBeginImageContext(drawImage.frame.size); 
[self.firma.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; 

和如果你不具備的狀態欄,刪除:currentPoint.y - = 20; 希望這是問題所在。

+0

其實,在乞討的應用程序它的工作fine.But在我的應用程序,我添加了一些卡通圖像。在添加此圖像作爲子視圖時,此畫線位置改變。這是問題.. – Nari 2012-02-28 15:38:11

+0

所以我做了一個遊戲與繪圖和添加圖像,我認爲這個問題是當你添加圖像,你可以發佈代碼這樣做? – 2012-02-28 16:14:45

相關問題