我需要通过使用默认委托方法触摸开始和触动移动来处理绘图应用程序。如何绘制精确的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();
}
}
感谢
得到了与您认为问题出在哪里相关的任何代码? – Joel 2012-02-28 12:49:02
你能展示一幅能帮助我们了解正在发生的事情的图像吗?或者你正在使用的代码来绘制? – sosborn 2012-02-28 12:49:30
你能请出示一部分代码吗? – HarshIT 2012-02-28 12:51:19