drawing dashed line using CALayer

后端 未结 8 1426
执笔经年
执笔经年 2020-12-04 16:49

I was able to draw a dashed box, using the following code:

CAShapeLayer *shapeLayer = [CAShapeLayer layer];
CGRect shapeRect = CGRectMake(0.0f, 0.0f, 200.0f,         


        
8条回答
  •  没有蜡笔的小新
    2020-12-04 17:43

    Got it working in Objective C with the simplified code as below

       //Dashed line for road
        CAShapeLayer *dashedLine = [CAShapeLayer layer];
        [dashedLine setFrame:CGRectMake(0, 342, 100 , 100)];
    
        // Setup the path
        CGMutablePathRef thePath = CGPathCreateMutable();
        CGPathMoveToPoint(thePath, NULL, 0, 10);
        CGPathAddLineToPoint(thePath, NULL, screenSize.width,10);
        dashedLine.path = thePath;
        CGPathRelease(thePath);
    
        [dashedLine setLineDashPattern: [NSArray arrayWithObjects:[NSNumber numberWithFloat:15], nil]];
        dashedLine.lineWidth = 1.0f;
        dashedLine.strokeColor =  [[UIColor redcolor] CGColor]];
    
        [self.view.layer addSublayer:dashedLine];
    

提交回复
热议问题