Objective-C中的垂直虚线。添加渐变色?

时间:2016-08-23 03:39:26

标签: ios objective-c

我想使用Objective-C创建一条垂直虚线。正版虚线,不是虚线。并且还想添加渐变色。如何进行?

答案:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//At first, draw a gradient line
CAGradientLayer *gLayer = [CAGradientLayer layer];
gLayer.frame = CGRectMake(50, 50, 10, 200);
gLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,
                  (__bridge id)[UIColor greenColor].CGColor];
[self.view.layer addSublayer:gLayer];

//Then, create a path for dots
CGMutablePathRef dotPath = CGPathCreateMutable();
UIBezierPath *part1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 10, 10)];
UIBezierPath *part2 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 15, 10, 10)];
UIBezierPath *part3 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 30, 10, 10)];
UIBezierPath *part4 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 45, 10, 10)];
UIBezierPath *part5 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 60, 10, 10)];
UIBezierPath *part6 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 75, 10, 10)];
UIBezierPath *part7 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 90, 10, 10)];
UIBezierPath *part8 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 105, 10, 10)];
UIBezierPath *part9 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 120, 10, 10)];
UIBezierPath *part10 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 135, 10, 10)];
CGPathAddPath(dotPath, nil, part1.CGPath);
CGPathAddPath(dotPath, nil, part2.CGPath);
CGPathAddPath(dotPath, nil, part3.CGPath);
CGPathAddPath(dotPath, nil, part4.CGPath);
CGPathAddPath(dotPath, nil, part5.CGPath);
CGPathAddPath(dotPath, nil, part6.CGPath);
CGPathAddPath(dotPath, nil, part7.CGPath);
CGPathAddPath(dotPath, nil, part8.CGPath);
CGPathAddPath(dotPath, nil, part9.CGPath);
CGPathAddPath(dotPath, nil, part10.CGPath);

//Finally, set mask layer to the gradient line
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = dotPath;
[gLayer setMask:maskLayer];
}

1 个答案:

答案 0 :(得分:0)

我只是根据你的描述写一个样本,所有的点都是由UIBezierPath创建的,你可以改变你想要的任何东西,看看:

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//At first, draw a gradient line
CAGradientLayer *gLayer = [CAGradientLayer layer];
gLayer.frame = CGRectMake(50, 50, 10, 200);
gLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,
                  (__bridge id)[UIColor greenColor].CGColor];
[self.view.layer addSublayer:gLayer];

//Then, create a path for dots
CGMutablePathRef dotPath = CGPathCreateMutable();
UIBezierPath *part1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 10, 10)];
UIBezierPath *part2 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 15, 10, 10)];
UIBezierPath *part3 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 30, 10, 10)];
UIBezierPath *part4 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 45, 10, 10)];
UIBezierPath *part5 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 60, 10, 10)];
UIBezierPath *part6 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 75, 10, 10)];
UIBezierPath *part7 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 90, 10, 10)];
UIBezierPath *part8 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 105, 10, 10)];
UIBezierPath *part9 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 120, 10, 10)];
UIBezierPath *part10 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 135, 10, 10)];
CGPathAddPath(dotPath, nil, part1.CGPath);
CGPathAddPath(dotPath, nil, part2.CGPath);
CGPathAddPath(dotPath, nil, part3.CGPath);
CGPathAddPath(dotPath, nil, part4.CGPath);
CGPathAddPath(dotPath, nil, part5.CGPath);
CGPathAddPath(dotPath, nil, part6.CGPath);
CGPathAddPath(dotPath, nil, part7.CGPath);
CGPathAddPath(dotPath, nil, part8.CGPath);
CGPathAddPath(dotPath, nil, part9.CGPath);
CGPathAddPath(dotPath, nil, part10.CGPath);

//Finally, set mask layer to the gradient line
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.path = dotPath;
[gLayer setMask:maskLayer];
}

希望它可以帮到你。