iOS 在view上画设虚线

前言:很多时候我们在做界面时,UI为了适配屏幕,防止出现拉伸效果,很多细线,不会给我们切图,而是让我们自己用代码画出来。现在我就来介绍一下如何在UIView上划出一条虚线。

//设置虚线

- (void)dottedLine

{

CAShapeLayer*shapeLayer = [CAShapeLayerlayer];

[shapeLayersetBounds:self.bounds];

[shapeLayersetPosition:self.center];

[shapeLayersetFillColor:[[UIColorclearColor]CGColor]];

//设置虚线颜色为

[shapeLayersetStrokeColor:[RGB(248,211,211)CGColor]];

// 1.0f设置虚线的宽度

[shapeLayersetLineWidth:1.0f];

[shapeLayersetLineJoin:kCALineJoinRound];

// 3=线的宽度3=每条线的间距

[shapeLayersetLineDashPattern:

[NSArrayarrayWithObjects:[NSNumbernumberWithInt:3],

[NSNumbernumberWithInt:3],nil]];

// Setup the path

CGMutablePathRefpath =CGPathCreateMutable();

// 100, 40代表的是虚线最终点坐标

CGPathMoveToPoint(path,NULL,100,40);

// Setup the path

// 100,0代表初始坐标的x,y

CGPathAddLineToPoint(path,NULL,100,0);

[shapeLayersetPath:path];

CGPathRelease(path);

[self.backView.layeraddSublayer:shapeLayer];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容