我已经制作了使用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];

此代码的输出是->

ios - 如何创建具有不同颜色的贝塞尔曲线路径?-LMLPHP

关于ios - 如何创建具有不同颜色的贝塞尔曲线路径?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41627542/

10-09 01:02
查看更多