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

Ios Objective-C中的垂直虚线。添加渐变颜色吗?,ios,objective-c,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 la

我想用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];
}

我只是根据您的描述写了一个示例,所有的点都是由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];
}

希望它能帮助你。

可能会有帮助。很高兴能帮助你,如果它解决了你的问题,你能将此标记为答案:),然后其他人可以知道它也能帮助他们。