[iOS 绘制虚线] 转: iOS 绘制虚线的三种方法

news/2024/7/20 20:09:28 标签: ios, quartz, uiview, 2d, 虚线

原作:http://blog.csdn.net/ashimar_a/article/details/53033361

quartz-2d-在-uiview-drawrect方法进行绘制虚线">方法一:通过Quartz 2D 在 UIView drawRect:方法进行绘制虚线

- (void)drawRect:(CGRect)rect { // 可以通过 setNeedsDisplay 方法调用 drawRect:
    // Drawing code

    CGContextRef context =UIGraphicsGetCurrentContext();
    // 设置线条的样式
    CGContextSetLineCap(context, kCGLineCapRound);
    // 绘制线的宽度
    CGContextSetLineWidth(context, 3.0);
    // 线的颜色
    CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].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 绘制虚线

/**
 *  通过 Quartz 2D 在 UIImageView 绘制虚线
 *
 *  param imageView 传入要绘制成虚线的imageView
 *  return
 */

- (UIImage *)drawLineOfDashByImageView:(UIImageView *)imageView {
    // 开始划线 划线的frame
    UIGraphicsBeginImageContext(imageView.frame.size);

    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];

    // 获取上下文
    CGContextRef line = UIGraphicsGetCurrentContext();

    // 设置线条终点的形状
    CGContextSetLineCap(line, kCGLineCapRound);
    // 设置虚线的长度 和 间距
    CGFloat lengths[] = {5,5};

    CGContextSetStrokeColorWithColor(line, [UIColor greenColor].CGColor);
    // 开始绘制虚线
    CGContextSetLineDash(line, 0, lengths, 2);

    CGContextMoveToPoint(line, 0.0, 2.0);

    CGContextAddLineToPoint(line, 300, 2.0);

    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 = [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];
}

http://www.niftyadmin.cn/n/1557368.html

相关文章

HTML contenteditable 属性

今天无意中看到这个属性,特此记录一下。 浏览器支持 所有主流浏览器都支持 contenteditable 属性 定义和用法 contenteditable 属性指定元素内容是否可编辑。 注意: 当元素中没有设置 contenteditable 属性时,元素将从父元素继承。 HTM…

Cookie或将被替换!Chrome工程师提议新型HTTP状态管理协议

问题 Cookie允许无状态的HTTP协议支持有状态会话,在web上,我们依靠Cookie实现了很多有趣的功能。即便如此,Cookie依然还是有很多问题:使用起来不够安全,浪费资源,使用一种令人惊讶的方式追踪用户在网络上的…

[iOS UIScrollerView 单向滚动] 只能向上滚动 不向下滚动

比如导航条颜色 和 scrollerview顶部的颜色相同,那么在向下滚动的时候 就会显出背景view,不太好看, 就需要限制scrollerview的滚动方向 ,通过代理来判断offset.y的大小 可以做出限制 #pragma mark - scrollerViewDelegate //只能向上滚动 -(void)scrollViewDidScroll:(UIScrol…

vue、js onSelect事件 获取选中的值

onSelect 事件 作用&#xff1a;当文本被选中时&#xff0c;执行一段 Javascript代码 定义和用法&#xff1a; onselect 事件会在文本框中的文本被选中时发生 onselect 在 HTML 中&#xff1a; onselect 属性可用于: <input type"file">, <input type…

Android空包Apk签名

前言&#xff1a;现在公司在提交到魅族开发者平台的时候需要认领应用时&#xff0c;需要给一个魅族的空白包签名&#xff0c;然后在提交上去审核。 现在主要的是如何的签名&#xff1f; 首先你要准备好你的JAVA_HOME和ANDROID_HOME这里我就不多说的了&#xff1b; 接下来就是签…

[iOS 自定义TableviewCell 按钮复用问题] 按钮选择 避免复用

先看需求 如上图,需求需要在一堆复用的自定义cell中 点击一个cell 在当前cell显示选中按钮 如果单纯的在cell里添加Btn再在didselect方法中去修改按钮状态,那么就会出现别的cell中也出现了一个选中的按钮. 这是由于cell的复用导致的 为了避免这个情况,这里的思路就是, 1…

vue element ui el-date-picker(日期选择器)实现联动联级选择效果。

需求&#xff1a; 页面上有三个 日期选则器。第一个只能选择月份&#xff0c;第二个是 年月&#xff0c; 第三个是年月日 。 然后第一个选择完毕 第二个、第三个自动带出年 。第二个选择月 第三个自动带出月。 效果图&#xff1a; 实现过程&#xff1a; 思路&#xff1a; 就…

vue 引入pinia报 injection “symbol(pinia)“ not found

报错信息&#xff1a; injection "symbol(pinia)" not found 解决方案&#xff1a; 根据报错&#xff1a;是在 app.vue 里 没引用 和使用 。 我一开始没有调用 app.use(pinia) &#xff1b;加上就好了。 import { createApp } from vue import { createPinia } …