2012-02-28 108 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

相关问题