iOS 画虚线

2017-01-06  本文已影响111人  旅途开发者

在所需要的view上画一条虚线,重写下面的方法即可

-(void)drawRect:(CGRect)rect{

[super drawRect:rect];

CGContextRef currentContext = UIGraphicsGetCurrentContext();

//设置虚线颜色

CGContextSetStrokeColorWithColor(currentContext, UIColorFromRGB(0x999999).CGColor);

//设置虚线宽度

CGContextSetLineWidth(currentContext, 1);

CGContextSetAlpha(currentContext, 1);

//设置虚线绘制起点

CGContextMoveToPoint(currentContext, 10, 49);

//设置虚线绘制终点

CGContextAddLineToPoint(currentContext, self.frame.origin.x + self.frame.size.width, 49);

//设置虚线排列的宽度间隔:下面的arr中的数字表示先绘制3个点再绘制1个点

CGFloat arr[] = {5,1};

//下面最后一个参数“2”代表排列的个数。

CGContextSetLineDash(currentContext, 0, arr, 2);

CGContextDrawPath(currentContext, kCGPathStroke);

}

上一篇 下一篇

猜你喜欢

热点阅读