/**
绘制虚线
@param lineView 需要绘制成虚线的view
@param lineLength 虚线的宽度
@param lineSpacing 虚线的间距
@param lineColor 虚线的颜色
*/
- (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor
{
// 根据lineView的宽高,决定虚线是水平方向还是竖直方向
BOOL isHorizontally = lineView.bounds.size.width > lineView.bounds.size.height;
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setBounds:lineView.bounds];
if (isHorizontally) {
[shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2,
CGRectGetHeight(lineView.frame))];
}else {
[shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame),
CGRectGetHeight(lineView.frame) / 2)];
}
[shapeLayer setFillColor:[UIColor clearColor].CGColor];
// 设置虚线颜色为blackColor
[shapeLayer setStrokeColor:lineColor.CGColor];
// 设置虚线宽度
if (isHorizontally) {
[shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
}else {
[shapeLayer setLineWidth:CGRectGetWidth(lineView.frame)];
}
[shapeLayer setLineJoin:kCALineJoinRound];
// 设置线宽,线间距
[shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
// 设置路径
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0, 0);
if (isHorizontally) {
CGPathAddLineToPoint(path, NULL, CGRectGetWidth(lineView.frame), 0);
}else {
CGPathAddLineToPoint(path, NULL, 0, CGRectGetHeight(lineView.frame));
}
[shapeLayer setPath:path];
CGPathRelease(path);
// 把绘制好的虚线添加上来
[lineView.layer addSublayer:shapeLayer];
}
在指定view上绘制虚线
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 转自:http://blog.csdn.net/lwklan/article/details/50588916 方...