iOS 绘制虚线的三种方法
2016-12-06 14:55
393 查看
方法一:通过Quartz 2D 在 UIView drawRect:方法进行绘制虚线
- (void)drawRect:(CGRect)rect {//
可以通过 setNeedsDisplay方法调用 drawRect:
// Drawing code
CGContextRef context =UIGraphicsGetCurrentContext();
// 设置线条的样式
CGContextSetLineCap(context,kCGLineCapRound);
// 绘制线的宽度
CGContextSetLineWidth(context,3.0);
// 线的颜色
CGContextSetStrokeColorWithColor(context, [UIColororangeColor].CGColor);
// 开始绘制
CGContextBeginPath(context);
// 设置虚线绘制起点
CGContextMoveToPoint(context,10.0,
20.0);
// lengths的值{10,10}表示先绘制10个点,再跳过10个点,如此反复
CGFloat lengths[] = {10,10};
// 虚线的起始点
CGContextSetLineDash(context,0, lengths,2);
// 绘制虚线的终点
CGContextAddLineToPoint(context,310.0,20.0);
// 绘制
CGContextStrokePath(context);
// 关闭图像
CGContextClosePath(context);
}
方法二:通过 Quartz 2D 在 UIImageView 绘制虚线
/**
* 通过 Quartz 2D在 UIImageView绘制虚线
*
* param imageView 传入要绘制成虚线的imageView
* return
*/
- (UIImage *)drawLineOfDashByImageView:(UIImageView *)imageView {
// 开始划线
划线的frame
CGRect frame = imageView.frame;
UIGraphicsBeginImageContext(frame.size);
[imageView.image
drawInRect:CGRectMake(0,
0, frame.size.width, frame.size.height)];
// 获取上下文
CGContextRef line =
UIGraphicsGetCurrentContext();
// 设置线条终点的形状
CGContextSetLineCap(line,
kCGLineCapRound);
// 设置虚线的长度
和
间距
CGFloat lengths[] = {5,5};
CGContextSetStrokeColorWithColor(line, [NSString
colorWithHexString:@"#806F29"].CGColor);
// 开始绘制虚线
CGContextSetLineDash(line,
0, lengths, 2);
CGContextMoveToPoint(line,
0.0,
2.0);
CGContextAddLineToPoint(line, frame.size.width, frame.size.height);
CGContextStrokePath(line);
// UIGraphicsGetImageFromCurrentImageContext()返回的就是image
return
UIGraphicsGetImageFromCurrentImageContext();
}
方法三:通过CAShapeLayer 方式绘制虚线
/**
* 通过 CAShapeLayer方式绘制虚线
*
* param lineView: 需要绘制成虚线的view
* param lineLength: 虚线的宽度
* param lineSpacing: 虚线的间距
* param lineColor: 虚线的颜色
**/
- (void)drawLineOfDashByCAShapeLayer:(UIView *)lineView lineLength:(int)lineLength
lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor {
CAShapeLayer *shapeLayer = [CAShapeLayerlayer];
[shapeLayer setBounds:lineView.bounds];
[shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame)
/ 2,
CGRectGetHeight(lineView.frame))];
[shapeLayer setFillColor:[UIColorclearColor].CGColor];
// 设置虚线颜色为blackColor
[shapeLayer setStrokeColor:lineColor.CGColor];
// 设置虚线宽度
[shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
[shapeLayer setLineJoin:kCALineJoinRound];
// 设置线宽,线间距
[shapeLayer setLineDashPattern:[NSArrayarrayWithObjects:[NSNumbernumberWithInt:lineLength],
[NSNumbernumberWithInt:lineSpacing],nil]];
// 设置路径
CGMutablePathRef path =CGPathCreateMutable();
CGPathMoveToPoint(path,NULL,
0,0);
CGPathAddLineToPoint(path,NULL,CGRectGetWidth(lineView.frame),0);
[shapeLayer setPath:path];
CGPathRelease(path);
// 把绘制好的虚线添加上来
[lineView.layeraddSublayer:shapeLayer];
}
- (void)drawRect:(CGRect)rect {//
可以通过 setNeedsDisplay方法调用 drawRect:
// Drawing code
CGContextRef context =UIGraphicsGetCurrentContext();
// 设置线条的样式
CGContextSetLineCap(context,kCGLineCapRound);
// 绘制线的宽度
CGContextSetLineWidth(context,3.0);
// 线的颜色
CGContextSetStrokeColorWithColor(context, [UIColororangeColor].CGColor);
// 开始绘制
CGContextBeginPath(context);
// 设置虚线绘制起点
CGContextMoveToPoint(context,10.0,
20.0);
// lengths的值{10,10}表示先绘制10个点,再跳过10个点,如此反复
CGFloat lengths[] = {10,10};
// 虚线的起始点
CGContextSetLineDash(context,0, lengths,2);
// 绘制虚线的终点
CGContextAddLineToPoint(context,310.0,20.0);
// 绘制
CGContextStrokePath(context);
// 关闭图像
CGContextClosePath(context);
}
方法二:通过 Quartz 2D 在 UIImageView 绘制虚线
/**
* 通过 Quartz 2D在 UIImageView绘制虚线
*
* param imageView 传入要绘制成虚线的imageView
* return
*/
- (UIImage *)drawLineOfDashByImageView:(UIImageView *)imageView {
// 开始划线
划线的frame
CGRect frame = imageView.frame;
UIGraphicsBeginImageContext(frame.size);
[imageView.image
drawInRect:CGRectMake(0,
0, frame.size.width, frame.size.height)];
// 获取上下文
CGContextRef line =
UIGraphicsGetCurrentContext();
// 设置线条终点的形状
CGContextSetLineCap(line,
kCGLineCapRound);
// 设置虚线的长度
和
间距
CGFloat lengths[] = {5,5};
CGContextSetStrokeColorWithColor(line, [NSString
colorWithHexString:@"#806F29"].CGColor);
// 开始绘制虚线
CGContextSetLineDash(line,
0, lengths, 2);
CGContextMoveToPoint(line,
0.0,
2.0);
CGContextAddLineToPoint(line, frame.size.width, frame.size.height);
CGContextStrokePath(line);
// UIGraphicsGetImageFromCurrentImageContext()返回的就是image
return
UIGraphicsGetImageFromCurrentImageContext();
}
方法三:通过CAShapeLayer 方式绘制虚线
/**
* 通过 CAShapeLayer方式绘制虚线
*
* param lineView: 需要绘制成虚线的view
* param lineLength: 虚线的宽度
* param lineSpacing: 虚线的间距
* param lineColor: 虚线的颜色
**/
- (void)drawLineOfDashByCAShapeLayer:(UIView *)lineView lineLength:(int)lineLength
lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor {
CAShapeLayer *shapeLayer = [CAShapeLayerlayer];
[shapeLayer setBounds:lineView.bounds];
[shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame)
/ 2,
CGRectGetHeight(lineView.frame))];
[shapeLayer setFillColor:[UIColorclearColor].CGColor];
// 设置虚线颜色为blackColor
[shapeLayer setStrokeColor:lineColor.CGColor];
// 设置虚线宽度
[shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
[shapeLayer setLineJoin:kCALineJoinRound];
// 设置线宽,线间距
[shapeLayer setLineDashPattern:[NSArrayarrayWithObjects:[NSNumbernumberWithInt:lineLength],
[NSNumbernumberWithInt:lineSpacing],nil]];
// 设置路径
CGMutablePathRef path =CGPathCreateMutable();
CGPathMoveToPoint(path,NULL,
0,0);
CGPathAddLineToPoint(path,NULL,CGRectGetWidth(lineView.frame),0);
[shapeLayer setPath:path];
CGPathRelease(path);
// 把绘制好的虚线添加上来
[lineView.layeraddSublayer:shapeLayer];
}
相关文章推荐
- iOS 绘制虚线的三种方法
- [iOS 绘制虚线] 转: iOS 绘制虚线的三种方法
- iOS绘制虚线方法
- IOS绘制虚线的方法总结
- iOS绘制虚线方法【原创】
- IOS绘制虚线的方法,可以给cell设置虚线分割线
- iOS绘制虚线方法
- iOS 图片拉伸的三种方法
- iOS三种Json方法解析国家气象局API
- IOS三种随机数方法
- iOS 设备唯一 ID 的三种替代方法之一
- ios开发-三种随机数方法
- ios 有如下三种随机数方法
- 一个绘制虚线的非常规函数(常规方法,打印机上绘制不出虚线)
- ios绘制虚线
- ios 拨打电话 三种方法
- iOS 开发之Resize UIimage的三种方法
- iOS三种Json方法解析国家气象局API
- 三种方法绘制矩形 源代码
- photoshop绘制虚线方法介绍