iOS ~ 贝塞尔曲线:二阶曲线图📈,ShapeLayer

原理:

// 起始点
- (void)moveToPoint:(CGPoint)point;
// 二阶曲线(两个控制点)
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
原理(这里的天气温度曲线):endPoint:下一个端点的x、y轴坐标, controlPoint1:X轴坐标是上一个startPoint.x和下一个endPoint.x的中间X坐标位置,Y轴是和上一个startPoint.y, controlPoint2:X轴坐标是上一个startPoint.x和下一个endPoint.x的中间X坐标位置,Y轴是和下一个endPoint.y,

关键代码:

if (k == 0) { // 第一个,特殊处理
    // 第一个点,起始点。
    [weather_MaxTempPath moveToPoint:CGPointMake([UIScreen mainScreen].bounds.size.width/375*67/2, [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * (nextTemp - minTemp)))];
            
    } else {
            
    GWCW_ClubWeather_DailyWeatherModel *dailyWeatherModel0 = self.dailyWeathers[k-1];
    CGFloat lastTemp = [dailyWeatherModel0.day_MaxTemp floatValue];
            
    // 原理(这里的天气温度曲线):endPoint:下一个端点的x、y轴坐标, controlPoint1:X轴坐标是上一个startPoint.x和下一个endPoint.x的中间X坐标位置,Y轴是和上一个startPoint.y, controlPoint2:X轴坐标是上一个startPoint.x和下一个endPoint.x的中间X坐标位置,Y轴是和下一个endPoint.y,
    [weather_MaxTempPath addCurveToPoint:CGPointMake([UIScreen mainScreen].bounds.size.width/375*(67/2 + 67*k) , [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * (nextTemp - minTemp)))
                        controlPoint1:CGPointMake([UIScreen mainScreen].bounds.size.width/375*(67/2 + 67*k - 67/2), [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * (lastTemp - minTemp)))
                        controlPoint2:CGPointMake([UIScreen mainScreen].bounds.size.width/375*(67/2 + 67*k - 67/2), [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * ((nextTemp - minTemp))))];
}
代码:
@property (nonatomic, strong) UIView        *top_LineRangeView; // 折线图📈的范围父view(温度)
@property (nonatomic, strong) UIView        *top_LineLayerRangeView; // 折线图📈的范围父view(折线图layer)
@property (nonatomic, strong) CAShapeLayer  *top_lineFatherLayer; // // 折线图的父layer
- (void)refreshTableFooterView {
    
    /** 未来15日的预报: */
    [self.top_LineRangeView removeFromSuperview];
    [self.top_LineLayerRangeView removeFromSuperview];
    [self.top_lineFatherLayer removeFromSuperlayer];
    
    // 为防止缺少数据,这里重新设置一下scrollview 的内容宽度:
    self.top_chartContentScrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width/375*(67*self.dailyWeathers.count), self.top_chartContentScrollView.frame.size.height);
    
    /**!!! 创建温度的背景view,(15天的 天气预览)  !!!*/
    _top_LineRangeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.top_chartContentScrollView.contentSize.width, [UIScreen mainScreen].bounds.size.width/375*120)];
    self.top_LineRangeView.backgroundColor = [UIColor clearColor];
    self.top_LineRangeView.userInteractionEnabled = NO;
    [self.top_chartContentView addSubview:self.top_LineRangeView];
    
    _top_LineLayerRangeView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.width/375*25, self.top_chartContentScrollView.contentSize.width, [UIScreen mainScreen].bounds.size.width/375*(120 - 50))];
    self.top_LineLayerRangeView.backgroundColor = [UIColor clearColor];
    self.top_LineLayerRangeView.userInteractionEnabled = NO;
    [self.top_chartContentView addSubview:self.top_LineLayerRangeView];
    
    
    /** 1、创建曲线图的父layer:120 - 50 = 70 */
    _top_lineFatherLayer = [[CAShapeLayer alloc] init];
    _top_lineFatherLayer.strokeColor = [UIColor clearColor].CGColor;
    
    UIBezierPath *bezierPath = [UIBezierPath
                                    bezierPathWithRoundedRect:CGRectMake(0, 0, self.top_chartContentScrollView.contentSize.width, [UIScreen mainScreen].bounds.size.width/375*(120 - 50))
                                    byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
                                    cornerRadii:CGSizeMake([UIScreen mainScreen].bounds.size.width/375*0, [UIScreen mainScreen].bounds.size.width/375*0)];

    _top_lineFatherLayer.lineWidth = [UIScreen mainScreen].bounds.size.width/375*0.01;
    // 颜色
    _top_lineFatherLayer.strokeColor = [UIColor clearColor].CGColor;
    // 背景填充色
    _top_lineFatherLayer.fillColor = [UIColor clearColor].CGColor;
    _top_lineFatherLayer.path = [bezierPath CGPath];
    [self.top_LineLayerRangeView.layer addSublayer:self.top_lineFatherLayer];
    
    /// 先找到15天的 最大温度和最低温度:(获取曲线图📈的范围)
    CGFloat maxTemp = 0;
    CGFloat minTemp = 0;
    
    NSMutableArray *temp_array = [NSMutableArray arrayWithCapacity:0];
    
    for (GWCW_ClubWeather_DailyWeatherModel *dailyWeatherModel in self.dailyWeathers) {
        NSString *day_MaxTemp = [NSString stringWithFormat:@"%@", dailyWeatherModel.day_MaxTemp];
        NSString *day_MinTemp = [NSString stringWithFormat:@"%@", dailyWeatherModel.day_MinTemp];
        // 15天内的所有最高温、最低温 都放到数组,(之下的for循环,获取全部温度中的最高温、最低温)
        [temp_array addObject:day_MaxTemp];
        [temp_array addObject:day_MinTemp];
    }
    
    for (int i = 0; i < temp_array.count; i++) {

        NSString *temp = [NSString stringWithFormat:@"%@", temp_array[i]];
        CGFloat i_temp = [temp floatValue];
        
//            NSLog(@"一天的温度😆😆 %.2f 😆😆", i_temp);
        if (i == 0) {
            maxTemp = i_temp;
            minTemp = i_temp;
        }

        if (maxTemp > i_temp) {
            maxTemp = maxTemp;
        } else {
            maxTemp = i_temp;
        }
        if (minTemp > i_temp) {
            minTemp = i_temp;
        } else {
            minTemp = minTemp;
        }
    }
    
    // 温度之差 的 温度范围:温度三种情况都是这个减法获取温度的范围
    CGFloat maxTempRange = maxTemp - minTemp;
    double maxTempRange_value = 0;
    if (maxTempRange == 0) {
        maxTempRange_value = 0;
    } else {
        maxTempRange_value = 70 / maxTempRange;
    }
    /** 最高温 path */
    UIBezierPath *weather_MaxTempPath = [UIBezierPath bezierPath];
    
    // 设置path的 起始点 和 其他点
    for (int k = 0; k < self.dailyWeathers.count; k++) {
        GWCW_ClubWeather_DailyWeatherModel *dailyWeatherModel = self.dailyWeathers[k];
        CGFloat nextTemp = [dailyWeatherModel.day_MaxTemp floatValue];
        
        // 两个相邻 端点 的位置间隔:67
        
        if (k == 0) { // 第一个,特殊处理
            // 第一个点,起始点。
            [weather_MaxTempPath moveToPoint:CGPointMake([UIScreen mainScreen].bounds.size.width/375*67/2, [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * (nextTemp - minTemp)))];
            
        } else {
            
            GWCW_ClubWeather_DailyWeatherModel *dailyWeatherModel0 = self.dailyWeathers[k-1];
            CGFloat lastTemp = [dailyWeatherModel0.day_MaxTemp floatValue];
            
            // 原理(这里的天气温度曲线):endPoint:下一个端点的x、y轴坐标, controlPoint1:X轴坐标是上一个startPoint.x和下一个endPoint.x的中间X坐标位置,Y轴是和上一个startPoint.y, controlPoint2:X轴坐标是上一个startPoint.x和下一个endPoint.x的中间X坐标位置,Y轴是和下一个endPoint.y,
            [weather_MaxTempPath addCurveToPoint:CGPointMake([UIScreen mainScreen].bounds.size.width/375*(67/2 + 67*k) , [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * (nextTemp - minTemp)))
                        controlPoint1:CGPointMake([UIScreen mainScreen].bounds.size.width/375*(67/2 + 67*k - 67/2), [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * (lastTemp - minTemp)))
                        controlPoint2:CGPointMake([UIScreen mainScreen].bounds.size.width/375*(67/2 + 67*k - 67/2), [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * ((nextTemp - minTemp))))];
        }
        
    }
    
    CAShapeLayer *maxTemp_Layer = [[CAShapeLayer alloc] init];
    // 线宽
    maxTemp_Layer.lineWidth = [UIScreen mainScreen].bounds.size.width/375*2;
    // 线条的颜色
    maxTemp_Layer.strokeColor = RGBA(255, 195, 85, 1).CGColor;
    // 背景填充色
    maxTemp_Layer.fillColor = [UIColor clearColor].CGColor;
    // 将UIBezierPath类转换成CGPath,类似于UIColor的CGColor
    maxTemp_Layer.path = [weather_MaxTempPath CGPath];
    [self.top_lineFatherLayer addSublayer:maxTemp_Layer];
    
    
    /** 最低温 path */
    UIBezierPath *weather_MinTempPath = [UIBezierPath bezierPath];
    
    // 设置path的 起始点 和 其他点
    for (int k = 0; k < self.dailyWeathers.count; k++) {
        GWCW_ClubWeather_DailyWeatherModel *dailyWeatherModel = self.dailyWeathers[k];
        CGFloat nextTemp = [dailyWeatherModel.day_MinTemp floatValue];
        
        // 两个相邻 端点 的位置间隔:67
        
        if (k == 0) { // 第一个,特殊处理
            // 第一个点,起始点。
            [weather_MinTempPath moveToPoint:CGPointMake([UIScreen mainScreen].bounds.size.width/375*67/2, [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * (nextTemp - minTemp)))];
            
        } else {
            
            GWCW_ClubWeather_DailyWeatherModel *dailyWeatherModel0 = self.dailyWeathers[k-1];
            CGFloat lastTemp = [dailyWeatherModel0.day_MinTemp floatValue];
            
            // 原理(这里的天气温度曲线):endPoint:下一个端点的x、y轴坐标, controlPoint1:X轴坐标是上一个startPoint.x和下一个endPoint.x的中间X坐标位置,Y轴是和上一个startPoint.y, controlPoint2:X轴坐标是上一个startPoint.x和下一个endPoint.x的中间X坐标位置,Y轴是和下一个endPoint.y,
            [weather_MinTempPath addCurveToPoint:CGPointMake([UIScreen mainScreen].bounds.size.width/375*(67/2 + 67*k) , [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * (nextTemp - minTemp)))
                        controlPoint1:CGPointMake([UIScreen mainScreen].bounds.size.width/375*(67/2 + 67*k - 67/2), [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * (lastTemp - minTemp)))
                        controlPoint2:CGPointMake([UIScreen mainScreen].bounds.size.width/375*(67/2 + 67*k - 67/2), [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * ((nextTemp - minTemp))))];
        }
        
    }
    
    CAShapeLayer *minTemp_Layer = [[CAShapeLayer alloc] init];
    // 线宽
    minTemp_Layer.lineWidth = [UIScreen mainScreen].bounds.size.width/375*2;
    // 线条的颜色
    minTemp_Layer.strokeColor = RGBA(84, 189, 255, 1).CGColor;
    // 背景填充色
    minTemp_Layer.fillColor = [UIColor clearColor].CGColor;
    // 将UIBezierPath类转换成CGPath,类似于UIColor的CGColor
    minTemp_Layer.path = [weather_MinTempPath CGPath];
    [self.top_lineFatherLayer addSublayer:minTemp_Layer];
    
    
    
    // 所有的曲线端点(小圆点)的位置:CGPoint
    NSMutableArray *max_circleArray = [NSMutableArray arrayWithCapacity:0];
    NSMutableArray *min_circleArray = [NSMutableArray arrayWithCapacity:0];
    for (int j = 0; j < self.dailyWeathers.count; j++) {
        
        GWCW_ClubWeather_DailyWeatherModel *dailyWeatherModel = self.dailyWeathers[j];
        CGFloat max_temp = [dailyWeatherModel.day_MaxTemp floatValue];
        CGFloat min_temp = [dailyWeatherModel.day_MinTemp floatValue];
        
        // 最高温
        if (j == 0) {
            
            // 端点位置CGPoint
            CGPoint circlePoint = CGPointMake([UIScreen mainScreen].bounds.size.width/375*(67/2), [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * (max_temp - minTemp)));
            
            NSValue* circleValue = [NSValue valueWithCGPoint:circlePoint];
            [max_circleArray addObject:circleValue];
        } else {
            CGPoint circlePoint = CGPointMake([UIScreen mainScreen].bounds.size.width/375*(67/2 + 67*j), [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * (max_temp - minTemp)));
            
            NSValue* circleValue = [NSValue valueWithCGPoint:circlePoint];
            [max_circleArray addObject:circleValue];
        }
        
        UILabel *max_TempL = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/375*(67*j), [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * (max_temp - minTemp)), [UIScreen mainScreen].bounds.size.width/375*67, [UIScreen mainScreen].bounds.size.width/375*16)];
        max_TempL.text = [NSString stringWithFormat:@"%@", dailyWeatherModel.day_MaxTemp];
        max_TempL.textColor = RGBA(0, 0, 0, 0.45);
        max_TempL.textAlignment = NSTextAlignmentCenter;
        max_TempL.font = [UIFont systemFontOfSize:[UIScreen mainScreen].bounds.size.width/375*15 weight:UIFontWeightRegular];
        [self.top_LineRangeView addSubview:max_TempL];
        
        
        
        // 最低温
        if (j == 0) {
            
            // 端点位置CGPoint
            CGPoint circlePoint = CGPointMake([UIScreen mainScreen].bounds.size.width/375*(67/2), [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * (min_temp - minTemp)));
            
            NSValue* circleValue = [NSValue valueWithCGPoint:circlePoint];
            [min_circleArray addObject:circleValue];
        } else {
            CGPoint circlePoint = CGPointMake([UIScreen mainScreen].bounds.size.width/375*(67/2 + 67*j), [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * (min_temp - minTemp)));
            
            NSValue* circleValue = [NSValue valueWithCGPoint:circlePoint];
            [min_circleArray addObject:circleValue];
        }
        
        UILabel *min_TempL = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/375*(67*j), [UIScreen mainScreen].bounds.size.width/375*(70 - maxTempRange_value * (min_temp - minTemp) + 16*2 + 1), [UIScreen mainScreen].bounds.size.width/375*67, [UIScreen mainScreen].bounds.size.width/375*16)];
        min_TempL.text = [NSString stringWithFormat:@"%@", dailyWeatherModel.day_MinTemp];
        min_TempL.textColor = RGBA(0, 0, 0, 0.45);
        min_TempL.textAlignment = NSTextAlignmentCenter;
        min_TempL.font = [UIFont systemFontOfSize:[UIScreen mainScreen].bounds.size.width/375*15 weight:UIFontWeightRegular];
        [self.top_LineRangeView addSubview:min_TempL];
       
    }
    
    self.top_CollectionView.hidden = NO;
    if (self.dailyWeathers.count > 0) {
        
        self.hourlyWeather = self.dailyWeathers[0].hourlyWeather;
        [self dayWeather:self.dailyWeathers[0]];
    }
    
    
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容