iOS 在view上画设虚线

2017-09-25  本文已影响0人  冯龙胜

前言:很多时候我们在做界面时,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];

}

上一篇下一篇

猜你喜欢

热点阅读