IOS 如何实现画虚线
2015-08-17 16:37
711 查看
因为项目需要画虚线,起初想用图片重复叠加实现。搜罗了一圈还是封装一个UIView
代码如下.h文件
#import <UIKit/UIKit.h> @interface DashesLineView : UIView @property(nonatomic)CGPoint startPoint;//虚线起点 @property(nonatomic)CGPoint endPoint;//虚线终点 @property(nonatomic,strong)UIColor* lineColor;//虚线颜色 @end
.m文件
#import "DashesLineView.h" #define kInterval 10 // 全局间距 @implementation DashesLineView - (id)initWithFrame:(CGRect)frame { self= [super initWithFrame:frame]; if(self) { _lineColor = [UIColor redColor]; _startPoint = CGPointMake(0, 1); _endPoint = CGPointMake(screen_width , 1); } return self; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextBeginPath(context); CGContextSetLineWidth(context,0.5);//线宽度 CGContextSetStrokeColorWithColor(context,self.lineColor.CGColor); CGFloat lengths[] = {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
相关文章推荐
- xamarin.ios Parse 模糊查询 查询
- 教你如何在iOS项目中设置各种字体
- 2014最新苹果开发者账号注册流程图解分享
- IOS 第三方支付的使用:支付宝
- iOS常见的格式输出类型
- ios 后退指定 跳转
- iOS 给任意一个view转换为image的方法封装
- iOS 文字属性
- iOS——常用开发工具
- iOS xcode 工程 项目 代码行数统计
- iOS开发 iOS8 分割线右移15像素 将其归零
- iOS SDK详解之IBInspectable和IB_DESIGNABLE-Storyboad动态刷新
- ios 基础数据类型
- 使用vmstat,iostat查看系统性能
- 【精】iOS GCD 详解
- ios弹出软键盘时fixed失效及点击空白处隐藏层的解决办法
- 30多个iOS常用动画,带详细注释
- [iOS]调用系统相机,相册,闪光灯
- iOS自动检查更新
- iOS开发系列--地图与定位