iOS中画虚线的方法【项目中可能会用到】
2016-08-31 11:47
225 查看
/**
** lineView: 需要绘制成虚线的view
** lineLength: 虚线的宽度
** lineSpacing: 虚线的间距
** lineColor: 虚线的颜色
**/
+ (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing
lineColor:(UIColor *)lineColor
{
CAShapeLayer *shapeLayer = [CAShapeLayer
layer];
[shapeLayer setBounds:lineView.bounds];
[shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame)
/ 2,
CGRectGetHeight(lineView.frame))];
[shapeLayer setFillColor:[UIColor
clearColor].CGColor];
// 设置虚线颜色为blackColor
[shapeLayer setStrokeColor:lineColor.CGColor];
// 设置虚线宽度
[shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
[shapeLayer setLineJoin:kCALineJoinRound];
// 设置线宽,线间距
[shapeLayer setLineDashPattern:[NSArray
arrayWithObjects:[NSNumber
numberWithInt:lineLength], [NSNumber
numberWithInt:lineSpacing],
nil]];
// 设置路径
CGMutablePathRef path =
CGPathCreateMutable();
CGPathMoveToPoint(path,
NULL, 0,
0);
CGPathAddLineToPoint(path,
NULL,
CGRectGetWidth(lineView.frame),
0);
[shapeLayer setPath:path];
CGPathRelease(path);
// 把绘制好的虚线添加上来
[lineView.layer
addSublayer:shapeLayer];
}
** lineView: 需要绘制成虚线的view
** lineLength: 虚线的宽度
** lineSpacing: 虚线的间距
** lineColor: 虚线的颜色
**/
+ (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing
lineColor:(UIColor *)lineColor
{
CAShapeLayer *shapeLayer = [CAShapeLayer
layer];
[shapeLayer setBounds:lineView.bounds];
[shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame)
/ 2,
CGRectGetHeight(lineView.frame))];
[shapeLayer setFillColor:[UIColor
clearColor].CGColor];
// 设置虚线颜色为blackColor
[shapeLayer setStrokeColor:lineColor.CGColor];
// 设置虚线宽度
[shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
[shapeLayer setLineJoin:kCALineJoinRound];
// 设置线宽,线间距
[shapeLayer setLineDashPattern:[NSArray
arrayWithObjects:[NSNumber
numberWithInt:lineLength], [NSNumber
numberWithInt:lineSpacing],
nil]];
// 设置路径
CGMutablePathRef path =
CGPathCreateMutable();
CGPathMoveToPoint(path,
NULL, 0,
0);
CGPathAddLineToPoint(path,
NULL,
CGRectGetWidth(lineView.frame),
0);
[shapeLayer setPath:path];
CGPathRelease(path);
// 把绘制好的虚线添加上来
[lineView.layer
addSublayer:shapeLayer];
}
相关文章推荐
- iOS项目中经常用到的小方法大全(持续更新)
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法。
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法。
- 项目中用到的关于时间段不能重复的解决方法
- .NET开发中你可能会用到的常用方法总结
- [转]项目中用到的dbx命令的简单使用方法
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法(摘自博客堂)
- .NET开发中你可能会用到的常用方法总结
- NET开发中你可能会用到的常用方法总结
- 项目中用到的三个绿色自动备份方法
- .NET开发中你可能会用到的常用方法总结
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法。
- 项目中用到的dbx命令的简单使用方法
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法。 (转)
- NET开发中你可能会用到的常用方法总结
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法。(转载)
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法
- 项目调试时候,出现其中用到的一个组件“访问被拒绝”的解决方法。
- .NET开发中你可能会用到的常用方法总结1
- .NET开发中你可能会用到的常用方法总结