iOS如何画虚线?☀️
- 版权声明:本文为博主原创文章,未经博主允许不得转载。
* 重写drawRect方法准备:
//继承UIView的子类.h文件
#import<UIKit/UIKit.h>
@interfaceDashesLineView :UIView
@property(nonatomic)CGPointstartPoint;//虚线起点
@property(nonatomic)CGPointendPoint;//虚线终点
@property(nonatomic,strong)UIColor* lineColor;//虚线颜色
@end```
######//.m文件
import"DashesLineView.h"
@implementationDashesLineView
-
(id)initWithFrame:(CGRect)frame
{
self= [superinitWithFrame:frame];
if(self)
{
// Initialization code}returnself;
}// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation. -
(void)drawRect:(CGRect)rect
{
CGContextRefcontext = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextSetLineWidth(context,0.5);//线宽度CGContextSetStrokeColorWithColor(context,self.lineColor.CGColor);
CGFloatlengths[] = {4,2};//先画4个点再画2个点
CGContextSetLineDash(context,0, lengths,2);//注意2(count)的值等于lengths数组的长度CGContextMoveToPoint(context,self.startPoint.x,self.startPoint.y);
CGContextAddLineToPoint(context,self.endPoint.x,self.endPoint.y);
CGContextStrokePath(context);CGContextClosePath(context);
}
@end```