您可以使用UIBezierPath的addClip方法来分割您的路径,并使用不同的颜色填充每个部分。以下是一个示例代码,演示如何使用BezierPath制作类似于山峰攀登的颜色编码:
- (void)drawRect:(CGRect)rect {
UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 200, 200)];
[[UIColor blackColor] setFill];
[path fill];
UIBezierPath *subpath1 = [UIBezierPath bezierPath];
[subpath1 moveToPoint:CGPointMake(0, 0)];
[subpath1 addLineToPoint:CGPointMake(0, 66)];
[subpath1 addLineToPoint:CGPointMake(33, 33)];
[subpath1 addLineToPoint:CGPointMake(66, 66)];
[subpath1 addLineToPoint:CGPointMake(39, 88)];
[subpath1 addLineToPoint:CGPointMake(0, 88)];
[subpath1 closePath];
[subpath1 addClip];
[[UIColor blueColor] setFill];
[path fill];
UIBezierPath *subpath2 = [UIBezierPath bezierPath];
[subpath2 moveToPoint:CGPointMake(66, 66)];
[subpath2 addLineToPoint:CGPointMake(100, 100)];
[subpath2 addLineToPoint:CGPointMake(100, 140)];
[subpath2 addLineToPoint:CGPointMake(66, 125)];
[subpath2 closePath];
[subpath2 addClip];
[[UIColor greenColor] setFill];
[path fill];
UIBezierPath *subpath3 = [UIBezierPath bezierPath];
[subpath3 moveToPoint:CGPointMake(100, 140)];
[subpath3 addLineToPoint:CGPointMake(120, 120)];
[subpath3 addLineToPoint:CGPointMake(140, 140)];
[subpath3 closePath];
[subpath3 add