我需要使用默认委托方法处理绘图应用程序触摸开始和触摸移动。
但是当我在屏幕的任意一点画一条线时,它们的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();
}
}
先谢谢
答案 0 :(得分: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; 希望这是问题。