您的位置:首页 > 移动开发 > IOS开发

iOS 绘制虚线

2015-12-10 10:58 337 查看
效果



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

-(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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: