zoukankan      html  css  js  c++  java
  • iOS绘制线条的使用

    1、相关简介

      1.1、iOS之UIBezierPath贝塞尔曲线属性简介

      1.2、iOS之CAShapeLayer属性简介

    2、绘制曲线

      2.1、方法详解

    - (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;

      追加一条二次贝塞尔曲线,结束点是endPoint,曲线偏向controlPoint控制点!  

      2.2、绘制曲线

    //绘制曲线
    - (void)drawView{
        UIBezierPath *path = [UIBezierPath bezierPath];
        DrawModel *firstModel = [self.dataArr firstObject];
        CGPoint firstPoint = firstModel.currPoint;
        [path moveToPoint:firstPoint];
        if (self.dataArr.count<2) {
            return;
        }
        for (int i=1; i<self.dataArr.count; i++) {
            DrawModel *seconModel = self.dataArr[i];
            CGPoint secondPoint = seconModel.currPoint;
            //中心点
            CGPoint midPoint = [self getMindPointWithFirstPoint:firstPoint secondPoint:secondPoint];
            //第一个点都是中心点,为了绘制两点之间有弧度
            [path addQuadCurveToPoint:midPoint controlPoint:[self getControlPointWithFirstPoint:midPoint SecondPoint:firstPoint]];
            [path addQuadCurveToPoint:secondPoint controlPoint:[self getControlPointWithFirstPoint:midPoint SecondPoint:secondPoint]];
            
            firstPoint = secondPoint;
        }
        CAShapeLayer  *layer = [CAShapeLayer layer];
        layer.lineWidth = 2;
        layer.strokeColor = [UIColor redColor].CGColor;
        layer.fillColor = [UIColor clearColor].CGColor;
        layer.path = [path CGPath];
        [self.backScrollView.layer addSublayer:layer];
    }
    //获取两点之间控制点
    - (CGPoint)getControlPointWithFirstPoint:(CGPoint)firstPoint
                                 SecondPoint:(CGPoint)secondPoint{
        
        CGPoint controlPoint = [self getMindPointWithFirstPoint:firstPoint secondPoint:secondPoint];
        
        CGFloat diffY = fabs(secondPoint.y - controlPoint.y);
        
        if (firstPoint.y < secondPoint.y)
            
            controlPoint.y += diffY;
        
        else if (firstPoint.y > secondPoint.y)
            
            controlPoint.y -= diffY;
        
        return controlPoint;
    }
    //获取两点之间中心点
    - (CGPoint)getMindPointWithFirstPoint:(CGPoint)firstPoint
                              secondPoint:(CGPoint)secondPoint{
        return CGPointMake((firstPoint.x + secondPoint.x)/2, (firstPoint.y + secondPoint.y)/2);
    }

    效果图

    3、绘制折线

    - (void)drawView{
        UIBezierPath *path = [UIBezierPath bezierPath];
        DrawModel *firstModel = [self.dataArr firstObject];
        CGPoint firstPoint = firstModel.currPoint;
        [path moveToPoint:firstPoint];
        if (self.dataArr.count<2) {
            return;
        }
        for (int i=1; i<self.dataArr.count; i++) {
            DrawModel *seconModel = self.dataArr[i];
            CGPoint secondPoint = seconModel.currPoint;
            [path addLineToPoint:secondPoint];
    
        }
        CAShapeLayer  *layer = [CAShapeLayer layer];
        layer.lineWidth = 2;
        layer.strokeColor = [UIColor redColor].CGColor;
        layer.fillColor = [UIColor clearColor].CGColor;
        layer.path = [path CGPath];
        [self.backScrollView.layer addSublayer:layer];
    }

    效果图

  • 相关阅读:
    php中常用的4种运行方式
    vue前后端分离项目,使用宝塔面板解决跨域问题,设置Nginx反向代理
    通过 Nginx 代理转发配置实现跨域(API 代理转发)
    ajax跨域,这应该是最全的解决方案了
    vue项目打包之后怎么在本地运行
    webpack打包vue项目之后生成的dist文件该怎么启动运行
    PHP7 windows增加自定义扩展和编译PHP源代码
    编写php自定义扩展
    PHP 扩展开发初探
    php实现伪静态以及定义原理
  • 原文地址:https://www.cnblogs.com/xianfeng-zhang/p/8601498.html
Copyright © 2011-2022 走看看