2016-08-23 105 views
-1

我想創建一個使用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]; 
} 
+0

[This](http://stackoverflow.com/questions/26018302/draw-dotted-not-dashed-line/31698463#31698463)可能會有所幫助。 – Gokul

回答

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]; 
} 

希望它能幫助你。

+0

是的謝謝。它的工作 –

+0

很高興爲您提供幫助,如果能解決您的問題,您能否將此標記爲答案:),然後其他人可以知道它也可以幫助他們。 –