2017-01-13 1 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를 쓰고있다. 객관적으로 C를 쓰겠 다. – hd1344

+0

내 다른 요구 조건은 다른 크기로 그려야한다는 것이다.하지만 경로의 크기를 바꿀 때 내 이전 경로의 크기는 그대로 남습니다. 도와주세요. . – hd1344

관련 문제