Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS中drawRect时如何设置不同的strokecolor_Ios_Objective C_Core Graphics - Fatal编程技术网

在iOS中drawRect时如何设置不同的strokecolor

在iOS中drawRect时如何设置不同的strokecolor,ios,objective-c,core-graphics,Ios,Objective C,Core Graphics,我想在“drawRect:”方法中绘制不同的strokecolor。这是我的密码: CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5); CGFloat radius = rect.size.width * 0.5; UIBezierPath *path = [UIBezierPath bezierPath]; path.lineCapStyle = kCGLineCapRound; path.l

我想在“drawRect:”方法中绘制不同的strokecolor。这是我的密码:

CGPoint center = CGPointMake(rect.size.width * 0.5, rect.size.height * 0.5);
CGFloat radius = rect.size.width * 0.5;
UIBezierPath *path = [UIBezierPath bezierPath];
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound;
CGContextRef ctx = UIGraphicsGetCurrentContext();
[kHomeSleepCellTrackColor set];
for (int i = 0; i < self.textArray.count*4; i++) {
    CGFloat angle = i / 48.0 * M_PI * 2; //48.0为总份数
    // 圆上的点  转换成iOS坐标
    CGFloat x = center.x + radius * cos(angle);
    CGFloat y = center.y + radius * sin(angle);
    CGFloat x0 = center.x + (radius - 1) * cos(angle);
    CGFloat y0 = center.y + (radius - 1) * sin(angle);

    [path moveToPoint:CGPointMake(x, y)];
    // 4的倍数就不画
    if (i % 4 == 0) {
        [path addLineToPoint:CGPointMake(x, y)];
    } else {
        [path addLineToPoint:CGPointMake(x0, y0)];
    }
    //刷新时要设置不同颜色
    if (i<24) {
        [kHomeSleepCellTrackColor set];
    } else {
        [kHomeSleepCellProgressColor set];
    }
}
CGContextAddPath(ctx, path.CGPath);
CGContextStrokePath(ctx);
CGPoint center=CGPointMake(rect.size.width*0.5,rect.size.height*0.5);
CGFloat半径=矩形尺寸宽度*0.5;
UIBezierPath*路径=[UIBezierPath bezierPath];
path.lineCapStyle=kCGLineCapRound;
path.lineJoinStyle=kCGLineJoinRound;
CGContextRef ctx=UIGraphicsGetCurrentContext();
[kHomeSleepCellTrackColor集合];
对于(int i=0;i如果(i当您调用
CGContextStrokePath
时,它使用最后设置的笔划颜色。给定路径只能使用单一颜色进行笔划(忽略渐变)

不能像正在尝试的那样设置路径各段的颜色

如果希望路径的不同部分具有不同的颜色,则需要创建多个路径,每个颜色至少对应一个路径


由于您只有两种颜色,因此可以创建两条路径。将线段添加到适当的路径中。然后在最后,添加并绘制两条路径中的每一条,并在绘制之前设置其颜色。

非常感谢,我已经按照您所说的创建了两条路径。它很有效。