【问题标题】:Draw multiple lines Core Graphics绘制多条线核心图形
【发布时间】:2015-07-29 06:58:04
【问题描述】:

我有一个可以绘制动画的自定义视图 - 只是不同的线条,每条线条都有不同的颜色。当用户输入信息时,这些行会出现在按钮处,并为不同的 UILabel 创建行。

我正在关注这个教程techotopia.com core graphics iOS 7 tutorial

它说要使 UIView 成为一个自定义类并将绘图放在 drawRect: 中 - 使用典型的上下文创建,添加线条、笔触、颜色等。好的,但是我如何获得不同颜色的多条线条同时绘制?

我需要为每一行创建图层吗?每行都有一个新的自定义类和视图?

如果我打电话给CGPathCloseSubpath会允许我创建一个新的起点吗?

如何更改新行的颜色,是否需要为新行创建新的上下文?

【问题讨论】:

    标签: ios objective-c core-graphics


    【解决方案1】:

    使用 UIBezierPath 比使用 CGPath 和 CGContext 更容易。

    -(void)drawRect:(CGRect)rect
    {
        UIBezierPath *line1 = [UIBezierPath bezierPath];
        [line1 moveToPoint:CGPointMake(0, 0)];
        [line1 addLineToPoint:CGPointMake(100, 0)];
        [[UIColor redColor] set]; //Set color to red
        [line1 stroke];
    
        UIBezierPath *line2 = [UIBezierPath bezierPath];
        [line2 moveToPoint:CGPointMake(0, 10)];
        [line2 addLineToPoint:CGPointMake(100, 10)];
        [[UIColor blueColor] set]; //Change color to blue
        [line2 stroke];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多