使用calayer画虚线

废话少说,上代码

- (void)drawDashLine {
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setFrame:CGRectMake(0, 0, self.halfLineView.frame.size.width, self.halfLineView.frame.size.height)];
    [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
    [shapeLayer setStrokeColor:[[UIColor whiteColor] CGColor]];
    [shapeLayer setLineWidth:1.0f];
    [shapeLayer setLineJoin:kCALineJoinRound];
    [shapeLayer setLineDashPattern:@[[NSNumber numberWithInt:3], [NSNumber numberWithInt:3]]];
    
    // Setup the path
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 1);
    CGPathAddLineToPoint(path, NULL, self.halfLineView.bounds.size.width, 1);
    
    [shapeLayer setPath:path];
    CGPathRelease(path);
    
    [self.halfLineView.layer addSublayer:shapeLayer];
}

希望使用的开心

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s