ios - CGContextStrokePath使用线条颜色

标签 ios objective-c core-graphics

我正在尝试绘制各有不同颜色的线条。 (但已连接)如果我不添加第二行并对其进行描边,则颜色为红色,但是当我添加第二行时,整个内容为黄色。如何使两条线显示不同的颜色?我的代码如下。

    CGPoint FirstCGPoint = [self pointForMapPoint:cr.points[0]];
    CGPathMoveToPoint(path, NULL, FirstCGPoint.x, FirstCGPoint.y);
    CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
    CGPoint SecondCGPoint = [self pointForMapPoint:cr.points[1]];
    CGPathAddLineToPoint(path, NULL, SecondCGPoint.x, SecondCGPoint.y);
    CGContextAddPath(context, path);
    CGContextStrokePath(context);

    CGContextSetStrokeColorWithColor(context, [[UIColor yellowColor] CGColor]);
    CGPoint NewFirstCGPoint = [self pointForMapPoint:cr.points[3]];
    CGPathAddLineToPoint(path, NULL, NewFirstCGPoint.x, NewFirstCGPoint.y);
    CGContextAddPath(context, path);
    CGContextStrokePath(context);

最佳答案

试试吧,您不需要使用路径。

CGPoint FirstCGPoint = CGPointMake(10.0f, 40.0f) ;
CGContextMoveToPoint(context, FirstCGPoint.x, FirstCGPoint.y) ;
CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]);
CGPoint SecondCGPoint = CGPointMake(10.0f, 100.0f) ;
CGContextAddLineToPoint(context, SecondCGPoint.x, SecondCGPoint.y);
CGContextStrokePath(context);

CGContextSetStrokeColorWithColor(context, [[UIColor yellowColor] CGColor]);
CGPoint NewFirstCGPoint = CGPointMake(100.0f, 100.0f) ;
CGContextMoveToPoint(context, SecondCGPoint.x, SecondCGPoint.y) ;
CGContextAddLineToPoint(context, NewFirstCGPoint.x, NewFirstCGPoint.y) ;
CGContextStrokePath(context);

关于ios - CGContextStrokePath使用线条颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20794729/

相关文章:

ios - 'xxx' 没有可见的 @interface 声明选择器 'init:'

ios - Xcode : Add attachment using picker to email

ios - 如何通过带有 Swift 的 UISlider 使用 CIColorControls 更改亮度、对比度和饱和度

iOS:setContentSize 导致我的整个 UIScrollView 框架跳转

ios - Xcode 6 符号不工作

ios - CGImage 蒙版不适用于宽色域显示器

ios - 将 Amazon API Gateway 与 ios 结合使用

ios - 仍然没有收到来自 fabric for twitter 的电子邮件

cocoa - 像 iOS 一样的 NSImage Overlay/Mask Gradient

iphone - 这个要怎么释放呢?