UIView with a Dashed line

后端 未结 8 1372
我寻月下人不归
我寻月下人不归 2020-11-28 06:22

What I have:

\"enter

To create this line, I basically have a

相关标签:
8条回答
  • 2020-11-28 06:56

    Update Swift 5 & UIBezierPath

    For those working with UIBezierPath instead of CAShapeLayer, here is how to achieve it

    class MyView: UIView {
    
        override func draw(_ rect: CGRect) {
    
            let path = UIBezierPath()
            // >> define the pattern & apply it 
            let dashPattern: [CGFloat] = [4.0, 4.0]
            path.setLineDash(dashPattern, count: dashPattern.count, phase: 0)
            // <<
            path.lineWidth = 1
            path.move(to: CGPoint(x: 0, y: 0))
            path.addLine(to: CGPoint(x: 100, y: 100))
            path.stroke()
    
        }
    }
    

    As said many times in this thread, you can play with the pattern and the phase to achieve a complex dotted line.

    Hope this helps

    0 讨论(0)
  • 2020-11-28 07:04

    Note: The code from Prince did really help me out, so I will give him +10 for the tips. But in the end, I add to come with my own code. I will also add some context to it, so it can be useful for future readers


    The final code was like this:

    -(void)updateLine{
    
          // Important, otherwise we will be adding multiple sub layers
          if ([[[self layer] sublayers] objectAtIndex:0])
            {
                self.layer.sublayers = nil;
            }
    
            CAShapeLayer *shapeLayer = [CAShapeLayer layer];
            [shapeLayer setBounds:self.bounds];
            [shapeLayer setPosition:self.center];
            [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
            [shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]];
            [shapeLayer setLineWidth:3.0f];
            [shapeLayer setLineJoin:kCALineJoinRound];
            [shapeLayer setLineDashPattern:
            [NSArray arrayWithObjects:[NSNumber numberWithInt:10],
            [NSNumber numberWithInt:5],nil]];
    
            // Setup the path
            CGMutablePathRef path = CGPathCreateMutable();
            CGPathMoveToPoint(path, NULL, beginPoint.center.x, beginPoint.center.y);
            CGPathAddLineToPoint(path, NULL, endPoint.center.x, endPoint.center.y);
    
            [shapeLayer setPath:path];
            CGPathRelease(path);
    
            [[self layer] addSublayer:shapeLayer];
    }
    

    In my case, the beginPoint and endPoint are movable by the user, by using KVO. So when one of them moves:

    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        if ([keyPath isEqual:@"position"])
        {
            [self updateLine];
        }
    }
    

    I did play a lot with Prince's code. I tried on the draw: method, which add a thin line between the dashed line (a bit weird...) and I also tried on initWithFrame:. By itself his code, without any modifications, would give me this kind of errors on the console:

    <Error>: CGContextSaveGState: invalid context 0x0
    <Error>: CGContextSetLineWidth: invalid context 0x0
    <Error>: CGContextSetLineJoin: invalid context 0x0
    <Error>: CGContextSetLineCap: invalid context 0x0
    <Error>: CGContextSetMiterLimit: invalid context 0x0
    <Error>: CGContextSetFlatness: invalid context 0x0
    <Error>: CGContextAddPath: invalid context 0x0
    <Error>: CGContextDrawPath: invalid context 0x0
    <Error>: CGContextRestoreGState: invalid context 0x0
    
    0 讨论(0)
提交回复
热议问题