iOS 绘制虚线

来源:互联网 发布:php实现注册登录 编辑:程序博客网 时间:2024/05/01 13:43

效果



绘制虚线的原理是先画一个层,然后给这个层添加一个间断的路径,让这个层按照这个路径显示,具体代码如下:

-(void)AddDottedLine{    //获取屏幕宽高    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;    CAShapeLayer *shapeLayer = [CAShapeLayer layer];    // 设置虚线颜色为黑色    [shapeLayer setStrokeColor:[UIColor blackColor].CGColor];    //设置虚线的宽度    [shapeLayer setLineWidth:10.0f];    //数组中前一项代表线的宽度,后一项代表每条线的间距    [shapeLayer setLineDashPattern:@[[NSNumber numberWithFloat:30.0f],[NSNumber numberWithFloat:10.0f]]];    //初始化虚线路径path    CGMutablePathRef path = CGPathCreateMutable();    //设置起点坐标 CGPathMoveToPoint(path, NULL, x, y);    CGPathMoveToPoint(path, NULL, 0, 0);    //设置终点坐标 CGPathMoveToPoint(path, NULL, x, y);    CGPathAddLineToPoint(path, NULL, screenWidth,screenHeight);    //给layer设置路径    [shapeLayer setPath:path];    CGPathRelease(path);    //将虚线添加到view的layer上    [self.view.layer addSublayer:shapeLayer];}


0 0
原创粉丝点击