mad*_*max 7 iphone xcode cgcontext uitouch
有没有一种简单的方法可以不在我的线条中绘制这些点?
我不知道为什么会出现这一点,因为我在绘制线条时从不将手指从屏幕上松开.

我从绘图示例中获取了代码.
// draw a line
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y -= 0; // 20 only for 'kCGLineCapRound'
UIGraphicsBeginImageContext(self.view.frame.size);
//Albert Renshaw - Apps4Life
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSize); // for size
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, alpha); //values for R, G, B, and Alpha
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
mouseMoved++;
if (mouseMoved == 10) {
mouseMoved = 0;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//Draw a dot
if(!mouseSwiped) {
UIGraphicsBeginImageContext(self.view.frame.size);
[drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brushSize);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, alpha);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
}
Run Code Online (Sandbox Code Playgroud)
这是最终版本,每行都有唯一的alpha,color,brushSize:
- (void) updateDrawingBoard
{
UIGraphicsBeginImageContext(self.drawImage.bounds.size);
for ( NSDictionary *dict in paths ) {
UIBezierPath *p = (UIBezierPath*)[dict objectForKey:@"path"];
p.lineWidth = [[dict objectForKey:@"size"]floatValue];
[[UIColor colorWithRed:[[dict objectForKey:@"red"]floatValue]
green:[[dict objectForKey:@"green"]floatValue]
blue:[[dict objectForKey:@"blue"]floatValue]
alpha:[[dict objectForKey:@"alpha"]floatValue]] setStroke];
[p stroke];
}
[[UIColor colorWithRed:r green:g blue:b alpha:alpha] setStroke];
[path stroke];
self.drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint touchPoint = [[touches anyObject] locationInView:self.drawImage];
path = [[UIBezierPath bezierPath] retain];
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinBevel;
path.lineWidth = brushSize;
[path moveToPoint:touchPoint];
[self updateDrawingBoard];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint touchPoint = [[touches anyObject] locationInView:self.drawImage];
[path addLineToPoint:touchPoint];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
path,@"path",
[NSNumber numberWithFloat:r], @"red",
[NSNumber numberWithFloat:g], @"green",
[NSNumber numberWithFloat:b], @"blue",
[NSNumber numberWithFloat:alpha], @"alpha",
[NSNumber numberWithFloat:brushSize], @"size", nil];
[paths addObject:dict];
[path release];
path = nil;
[self updateDrawingBoard];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint touchPoint = [[touches anyObject] locationInView:self.drawImage];
[path addLineToPoint:touchPoint];
[self updateDrawingBoard];
}
Run Code Online (Sandbox Code Playgroud)
问题是你通过绘制图像逐渐累积线条.旧线和新线重叠的地方,你会看到透支的"点".
解决方案是在路径中累积您的点,并使用该路径每次重新绘制图像.由于您将绘制单个路径,而不是多个重叠路径,因此您不应该看到这些点.
代码大纲:
CGMutablePathRef.CGPathAddLineToPoint.CGContextAddPath将线条添加到上下文中,然后根据需要填充或描边.你也可以用CGContextDrawPath.或者,您可以使用UIBezierPath而不是CGMutablePathRef,在这种情况下,步骤是:
UIBezierPath.-addLineToPoint:向路径添加行.-stroke,-fill和类似的方法将路径绘制到上下文中.如果您不习惯直接使用CoreGraphics,这可能会更简单.
我将下降中间图像和该代码移动到视图的类(LineDrawView或CanvasView或东西),而不是离开代码中的视图控制器.视图可以直接将自己绘制到screene,而不是更新图像.视图将具有清除路径,撤消笔划和创建路径图像的方法.然后视图控制器将使用它们来清除画布,撤消线条并保存图形.稍后您可以通过配置线条样式的功能来丰富它.
| 归档时间: |
|
| 查看次数: |
2227 次 |
| 最近记录: |