多色线描

时间:2013-09-22 17:54:14

标签: iphone ios objective-c ipad

我正在创建一个绘图应用。其中一个功能是多色线条绘制。它应该像用户触摸屏幕并在其上画线一样。线条颜色变化平稳。就像http://www.examples.pavelgatilov.com/Screen%20Shot%202013-09-22%20at%208.37.42%20PM.png

一样

我尝试了几种方法,但并不幸运。

我的画线方法如下:

- (void) drawLineFrom:(CGPoint)from to:(CGPoint)to width:(CGFloat)width
{
self.drawColor = toolColor;
UIGraphicsBeginImageContext(self.frame.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextScaleCTM(ctx, 1.0f, -1.0f);
CGContextTranslateCTM(ctx, 0.0f, -self.frame.size.height);
if (drawImage != nil) {
    CGRect rect = CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height);
    CGContextDrawImage(ctx, rect, drawImage.CGImage);
}
CGContextSetLineCap(ctx, kCGLineCapRound);
CGContextSetLineWidth(ctx, width);
CGContextSetStrokeColorWithColor(ctx, self.drawColor.CGColor);
CGContextMoveToPoint(ctx, from.x, from.y);
CGContextAddLineToPoint(ctx, to.x, to.y);
CGContextStrokePath(ctx);
CGContextFlush(ctx);
drawImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
drawLayer.contents = (id)drawImage.CGImage;
}

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

根据确切的颜色,它们的变化方式以及您想要的效果/线条中的变化情况,您可能需要查看以下组合:

  • CGContextDrawLinearGradient屏蔽了用户绘制的路径。
  • colorWithPatternImage:
  • CGContextDrawLinearGradient在另一个图层后面绘制,并使用kCGBlendModeClear
  • 将透明度绘制到顶层
相关问题