2017-01-13 12 views
1

UIBezierオブジェクトを使用してランダムパスを作成するサンプルデモを作成しました。 私は既に質問された同じ種類の質問をしたことを知っていますが、私はそれを解決することができませんでした。異なる色のベジェパスを作成する方法は?

コード

@implementation RandomShape 
@synthesize randomPath,size,color; 

- (void)drawRect:(CGRect)rect { 

    self.size = 1.0; 
    [self.color setStroke]; 
    [self.randomPath stroke]; 
} 

-(id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if(self) 
    { 
     self.randomPath = [UIBezierPath bezierPath]; 
     [self.randomPath stroke]; 
    } 
    return self; 
} 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 
    [self.randomPath moveToPoint:[touch locationInView:self]]; 
     [self.randomPath setLineWidth:size]; 
    [self setNeedsDisplay]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *mytouch=[touches anyObject]; 
    [self.randomPath addLineToPoint:[mytouch locationInView:self]]; 
    [self setNeedsDisplay]; 
} 
-(void)clearRandomShape 
{ 
    self.randomPath = nil; //Set current path nil 
    self.randomPath = [UIBezierPath bezierPath]; //Create new path 
    [self.randomPath setLineWidth:2.0]; 
    [self setNeedsDisplay]; 
} 

1)私はピッカービューから色を選択しています。私はそれがあるとして、それはすべての前の行の色を変更ピッカーからカラーを選択した場合> -

今、私の問題は、

です。

(それは私がすべてのランダムなパスで選択した最後の色が表示されます。)

--->私の要件は、私は別の異なる色のランダムなパスをワンドです。

私を混乱させてください。

ありがとうございます。

+0

は、複数のセグメントであなたのラインを描き、あなたの選択に、各セグメントを色付け?私も覚えている[CAGradientLayer](https://developer.apple.com/reference/quartzcore/cagradientlayer)灰色で色を使用することができます –

+0

私の別の要件は、私は別の異なるサイズで描画したいです。しかし、私は変更パスの大きさ私の古いパスの大きさはそのままです。よろしくお願いします。 – hd1344

+0

これはまさに私が意図したことです。ユーザーが 'touchesBegan'で描画するたびに、既存のパスに行を追加するのではなく、そのパスの新しいプロパティで新しいパスを作成します。単一の 'UIBezierPath'の代わりに、' UIBezierPath'を維持し、それを 'Array'します。 Btw、私はswift3でコード化しているので正式な答えを提供していない、私がobjCで謝罪しようとすると構文エラーを起こす可能性がある。 –

答えて

1

書き込みパスを作成したい場所にこのコード..このコードの

//path 1 
UIBezierPath *linePath = [[UIBezierPath alloc] init]; 
[linePath moveToPoint:CGPointMake(100, 100)]; 
[linePath addLineToPoint:CGPointMake(275, 100)]; 

CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 
shapeLayer.strokeColor = [UIColor redColor].CGColor; 
shapeLayer.fillColor = [UIColor clearColor].CGColor; 
shapeLayer.lineWidth = 2; 
shapeLayer.lineJoin = kCALineJoinRound; 
shapeLayer.lineCap = kCALineCapRound; 
shapeLayer.path = linePath.CGPath; 
[self.view.layer addSublayer:shapeLayer]; 

//Path 2 
UIBezierPath *verticalLinePath = [[UIBezierPath alloc] init]; 
[verticalLinePath moveToPoint:CGPointMake(100, 200)]; 
[verticalLinePath addLineToPoint:CGPointMake(275, 200)]; 
CAShapeLayer *horizontalLayer = [CAShapeLayer layer]; 
horizontalLayer.strokeColor = [UIColor greenColor].CGColor; 
horizontalLayer.fillColor = [UIColor clearColor].CGColor; 
horizontalLayer.lineWidth = 2; 
horizontalLayer.lineJoin = kCALineJoinRound; 
horizontalLayer.lineCap = kCALineCapRound; 
horizontalLayer.path = verticalLinePath.CGPath; 
[self.view.layer addSublayer:horizontalLayer]; 

//Path 
UIBezierPath *path3 = [[UIBezierPath alloc] init]; 
[path3 moveToPoint:CGPointMake(100, 300)]; 
[path3 addLineToPoint:CGPointMake(275, 300)]; 
CAShapeLayer *horizontalLayer3 = [CAShapeLayer layer]; 
horizontalLayer3.strokeColor = [UIColor blueColor].CGColor; 
horizontalLayer3.fillColor = [UIColor cyanColor].CGColor; 
horizontalLayer3.lineWidth = 2; 
horizontalLayer3.lineJoin = kCALineJoinRound; 
horizontalLayer3.lineCap = kCALineCapRound; 
horizontalLayer3.path = path3.CGPath; 
[self.view.layer addSublayer:horizontalLayer3]; 

出力がある - >

it looks like this

+0

もしあなたがそれを見つけたら、私が作った[GraphViews](https://github.com/BenOngKaiXai/GraphViews)を見てみたいかもしれませんが、それはユーザーフレンドリーではありませんが、使用する図面を描画する。 –

+0

私は客観的なCのwanで、私は客観的なcのsuggetionを与えてください – hd1344

+0

私は別の異なるサイズで描画したいです。しかし、私はパスのサイズを変更すると私の古いパスのサイズはそのままです。 。 – hd1344

関連する問題