//划线
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
//获取当前的上下文
CGContextRef content = UIGraphicsGetCurrentContext();
//设置线宽
CGContextSetLineWidth(content, 2.5);
//设置移动的点
CGContextMoveToPoint(content, 100, 100);
CGContextAddLineToPoint(content, 300, 100);
//设置线的颜色
[[UIColor redColor] setStroke];
//表示先绘制10个点,在跳过 10 个点,如此反复
//注意:count的值等于lengths数组的长度
//phase 参数表示在第一个虚线绘制的时候跳过多少个点
//CGContextSetLineDash(<#CGContextRef _Nullable c#>, <#CGFloat phase#>, <#const CGFloat * _Nullable lengths#>,\ <#size_t count#>)
CGFloat ss[] = {10 , 10};
CGContextSetLineDash(content, 5, ss, 2);
CGContextDrawPath(content, kCGPathFillStroke);
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
//获取当前的上下文
CGContextRef content = UIGraphicsGetCurrentContext();
//设置线宽
CGContextSetLineWidth(content, 2.5);
//设置移动的点
CGContextMoveToPoint(content, 100, 100);
CGContextAddLineToPoint(content, 300, 100);
//设置线的颜色
[[UIColor redColor] setStroke];
//表示先绘制10个点,在跳过 10 个点,如此反复
//注意:count的值等于lengths数组的长度
//phase 参数表示在第一个虚线绘制的时候跳过多少个点
//CGContextSetLineDash(<#CGContextRef _Nullable c#>, <#CGFloat phase#>, <#const CGFloat * _Nullable lengths#>,\ <#size_t count#>)
CGFloat ss[] = {10 , 10};
CGContextSetLineDash(content, 5, ss, 2);
CGContextDrawPath(content, kCGPathFillStroke);
}
本文介绍如何使用Objective-C在iOS应用中实现自定义视图的虚线绘制功能。通过CGContextSetLineDash方法设置虚线样式,具体展示了如何设置线宽、颜色及虚线的间隔与长度。
1万+

被折叠的 条评论
为什么被折叠?



