zoukankan      html  css  js  c++  java
  • iOS划虚线

    瓜娃子们在开发中 会遇到画虚线的问题,下面粘一段代码 一目了然:

    - (void)drawRect:(CGRect)rect

    {

        [super drawRect:rect];

        [self drawLine];

    }

    -(void)drawLine

    {

        CGContextRef context =UIGraphicsGetCurrentContext();

        CGContextBeginPath(context);

        CGContextSetLineWidth(context, 0.5);

        CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);

        CGFloat lengths[] = {10,10};

        CGContextSetLineDash(context, 0, lengths,2);

        CGContextMoveToPoint(context, 30, 89);

        CGContextAddLineToPoint(context, QDScreenWidth,89);

        CGContextStrokePath(context);

        CGContextClosePath(context);

    }

    画虚线需要用到函数:

    CGContextSetLineDash

    此函数需要四个参数:

    • context – 这个不用多说
    • phase - 稍后再说
    • lengths – 指明虚线是如何交替绘制,具体看例子
    • count – lengths数组的长度
    1. CGContextRef context =UIGraphicsGetCurrentContext();  
    2. CGContextBeginPath(context);  
    3. CGContextSetLineWidth(context, 2.0);  
    4. CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);  
    5. CGFloat lengths[] = {10,10};  
    6. CGContextSetLineDash(context, 0, lengths,2);  
    7. CGContextMoveToPoint(context, 10.0, 20.0);  
    8. CGContextAddLineToPoint(context, 310.0,20.0);  
    9. CGContextStrokePath(context);  
    10. CGContextClosePath(context);  

    lengths的值{10,10}表示先绘制10个点,再跳过10个点,如此反复,如图:

    如果把lengths值改为{10, 20, 10},则表示先绘制10个点,跳过20个点,绘制10个点,跳过10个点,再绘制20个点,如此反复,如图:

    注意count的值等于lengths数组的长度

    phase参数表示在第一个虚线绘制的时候跳过多少个点,举例说明:

    1. CGFloat lengths[] = {10,5};  
    2. CGContextSetLineDash(context, 0, lengths, 2);    
    3. CGContextMoveToPoint(context, 0.0, 20.0);    
    4. CGContextAddLineToPoint(context, 310.0, 20.0);     
    5. CGContextStrokePath(context);  
    6.                           
    7. CGContextSetLineDash(context, 5, lengths, 2);  
    8. CGContextMoveToPoint(context, 0.0, 40.0);    
    9. CGContextAddLineToPoint(context, 310.0, 40.0);  
    10. CGContextStrokePath(context);             
    11.                                               
    12. CGContextSetLineDash(context, 8, lengths, 2);     
    13. CGContextMoveToPoint(context, 0.0, 60.0);             
    14. CGContextAddLineToPoint(context, 310.0, 60.);             
    15. CGContextStrokePath(context);   

    如图显示:

    由于lengths值为{10,5},第一条线就是绘制10,跳过5,反复绘制。

    第二条线的phase值为5,则首先绘制【10减去5】,再跳过5,绘制10,反复绘制。

    第三条给也如此,先绘制2,再跳过5,如此反复。

  • 相关阅读:
    160309_Qt Essentials
    160309_Qt Reference Documentation
    160308_Signals & Slots
    160308_Helloworld_Gui Application
    网络爬虫(14)-动态页面爬取
    数据分析(6)-Pandas日期数据处理
    mysql基础(2)-excel功能在excel中如何实现?
    数据分析(5)-数据可视化常用图表类型和使用场景
    财经数据(6)-Python多进程爬虫东方财富个股盘口异动数据
    财经数据(5)-开盘啦股票标签数据爬虫
  • 原文地址:https://www.cnblogs.com/BinZone/p/4786754.html
Copyright © 2011-2022 走看看