2016-08-23 9 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

あなたを助けてうれしいですが、それがあなたの問題を解決したら答えとしてマークすることができます:)その後、他の人はそれも彼らを助けることができることを知ることができます。 –

関連する問題