drawing dashed line using CALayer

后端 未结 8 1359
执笔经年
执笔经年 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:46

    Swift, more compact:

    func addDashedLine(fromPoint start: CGPoint, toPoint end:CGPoint) {
        let line = CAShapeLayer()
        let linePath = UIBezierPath()
        linePath.moveToPoint(start)
        linePath.addLineToPoint(end)
        line.path = linePath.CGPath
        line.strokeColor = UIColor.redColor().CGColor
        line.lineWidth = 1
        line.lineJoin = kCALineJoinRound
        line.lineDashPattern = [4, 4]
        self.layer.addSublayer(line)
    }
    
    0 讨论(0)
  • 2020-12-04 17:49
    CAShapeLayer *shaplayer = [CAShapeLayer layer];
        shaplayer.frame = CGRectMake(100, 100, 100, 100);
        [self.view.layer addSublayer:shaplayer];
        UIBezierPath *uipath = [UIBezierPath bezierPath];
        [uipath moveToPoint:CGPointMake(50, 200)];
        [uipath addLineToPoint:CGPointMake(250, 200)];
        shaplayer.path = uipath.CGPath;
        shaplayer.strokeColor = [UIColor redColor].CGColor;
        shaplayer.lineDashPattern = @[@4];
    
    0 讨论(0)
提交回复
热议问题