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```

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

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,237评论 30 472
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,002评论 25 709
  • 好久没写东西了,得让脑袋活动活动写一篇了 最近忙着写微信小程序,对前端那一套更加熟练了,不得不说前端真是好上手,假...
    琴鸦阅读 246评论 0 0
  • 想到(知道)、学到、做到,各自之间有着很远的距离,形象地讲,相差一个巴菲特,或者无数个李笑来…… 成长究竟是什么呢...
    余良阅读 207评论 1 3