簡體   English   中英

如何創建不同顏色的貝塞爾路徑?

[英]How to Create Bezier path with different different colour?

我已經制作了使用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)我從選擇器視圖中選擇了顏色。

現在我的問題是,

-->當我從選擇器中選擇顏色時,它會按原樣更改所有以前的線條顏色。

(它顯示我在所有隨機路徑中選擇的最后一種顏色。)

--->我的要求是我魔杖不同顏色的隨機路徑。

請幫助我我很困惑。

謝謝你。

在您想要創建路徑的任何地方編寫此代碼..

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

此代碼的輸出是 ->

它看起來像這樣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM