虚线的实现


转载:http://blog.csdn.net/zh198964/article/details/47727053


因为项目需要画虚线,起初想用图片重复叠加实现。搜罗了一圈还是封装一个UIView

代码如下

.h文件
[objc]  view plain  copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface DashesLineView : UIView  
  4.   
  5. @property(nonatomic)CGPoint startPoint;//虚线起点  
  6.   
  7. @property(nonatomic)CGPoint endPoint;//虚线终点  
  8.   
  9. @property(nonatomic,strong)UIColor* lineColor;//虚线颜色  
  10.   
  11. @end  

.m文件

[objc]  view plain  copy
  1. #import "DashesLineView.h"  
  2.   
  3. #define kInterval 10                                // 全局间距  
  4.   
  5. @implementation DashesLineView  
  6.   
  7. - (id)initWithFrame:(CGRect)frame  
  8. {  
  9.     self= [super initWithFrame:frame];  
  10.     if(self) {  
  11.         _lineColor = [UIColor redColor];  
  12.         _startPoint = CGPointMake(01);  
  13.         _endPoint = CGPointMake(screen_width , 1);  
  14.           
  15.           
  16.     }  
  17.     return self;  
  18. }  
  19.   
  20. - (void)drawRect:(CGRect)rect {  
  21.       
  22.     CGContextRef context = UIGraphicsGetCurrentContext();  
  23.       
  24.     CGContextBeginPath(context);  
  25.       
  26.     CGContextSetLineWidth(context,0.5);//线宽度  
  27.       
  28.     CGContextSetStrokeColorWithColor(context,self.lineColor.CGColor);  
  29.       
  30.     CGFloat lengths[] = {4,2};//先画4个点再画2个点  
  31.       
  32.     CGContextSetLineDash(context,0, lengths,2);//注意2(count)的值等于lengths数组的长度  
  33.       
  34.     CGContextMoveToPoint(context,self.startPoint.x,self.startPoint.y);  
  35.       
  36.     CGContextAddLineToPoint(context,self.endPoint.x,self.endPoint.y);  
  37.       
  38.     CGContextStrokePath(context);  
  39.       
  40.     CGContextClosePath(context);  
  41.       
  42. }  
  43.   
  44.   
  45. @end  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值