原理:
// 起始點(diǎn)
- (void)moveToPoint:(CGPoint)point;
// 二階曲線(兩個(gè)控制點(diǎn))
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
原理(這里的天氣溫度曲線):endPoint:下一個(gè)端點(diǎn)的x、y軸坐標(biāo), controlPoint1:X軸坐標(biāo)是上一個(gè)startPoint.x和下一個(gè)endPoint.x的中間X坐標(biāo)位置,Y軸是和上一個(gè)startPoint.y, controlPoint2:X軸坐標(biāo)是上一個(gè)startPoint.x和下一個(gè)endPoint.x的中間X坐標(biāo)位置,Y軸是和下一個(gè)endPoint.y,
關(guān)鍵代碼:
if (k == 0) { // 第一個(gè),特殊處理
// 第一個(gè)點(diǎn),起始點(diǎn)。
[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:下一個(gè)端點(diǎn)的x、y軸坐標(biāo), controlPoint1:X軸坐標(biāo)是上一個(gè)startPoint.x和下一個(gè)endPoint.x的中間X坐標(biāo)位置,Y軸是和上一個(gè)startPoint.y, controlPoint2:X軸坐標(biāo)是上一個(gè)startPoint.x和下一個(gè)endPoint.x的中間X坐標(biāo)位置,Y軸是和下一個(gè)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日的預(yù)報(bào): */
[self.top_LineRangeView removeFromSuperview];
[self.top_LineLayerRangeView removeFromSuperview];
[self.top_lineFatherLayer removeFromSuperlayer];
// 為防止缺少數(shù)據(jù),這里重新設(shè)置一下scrollview 的內(nèi)容寬度:
self.top_chartContentScrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width/375*(67*self.dailyWeathers.count), self.top_chartContentScrollView.frame.size.height);
/**!!! 創(chuàng)建溫度的背景view,(15天的 天氣預(yù)覽) !!!*/
_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、創(chuàng)建曲線圖的父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天內(nèi)的所有最高溫、最低溫 都放到數(shù)組,(之下的for循環(huán),獲取全部溫度中的最高溫、最低溫)
[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;
}
}
// 溫度之差 的 溫度范圍:溫度三種情況都是這個(gè)減法獲取溫度的范圍
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];
// 設(shè)置path的 起始點(diǎn) 和 其他點(diǎn)
for (int k = 0; k < self.dailyWeathers.count; k++) {
GWCW_ClubWeather_DailyWeatherModel *dailyWeatherModel = self.dailyWeathers[k];
CGFloat nextTemp = [dailyWeatherModel.day_MaxTemp floatValue];
// 兩個(gè)相鄰 端點(diǎn) 的位置間隔:67
if (k == 0) { // 第一個(gè),特殊處理
// 第一個(gè)點(diǎn),起始點(diǎn)。
[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:下一個(gè)端點(diǎn)的x、y軸坐標(biāo), controlPoint1:X軸坐標(biāo)是上一個(gè)startPoint.x和下一個(gè)endPoint.x的中間X坐標(biāo)位置,Y軸是和上一個(gè)startPoint.y, controlPoint2:X軸坐標(biāo)是上一個(gè)startPoint.x和下一個(gè)endPoint.x的中間X坐標(biāo)位置,Y軸是和下一個(gè)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類轉(zhuǎn)換成CGPath,類似于UIColor的CGColor
maxTemp_Layer.path = [weather_MaxTempPath CGPath];
[self.top_lineFatherLayer addSublayer:maxTemp_Layer];
/** 最低溫 path */
UIBezierPath *weather_MinTempPath = [UIBezierPath bezierPath];
// 設(shè)置path的 起始點(diǎn) 和 其他點(diǎn)
for (int k = 0; k < self.dailyWeathers.count; k++) {
GWCW_ClubWeather_DailyWeatherModel *dailyWeatherModel = self.dailyWeathers[k];
CGFloat nextTemp = [dailyWeatherModel.day_MinTemp floatValue];
// 兩個(gè)相鄰 端點(diǎn) 的位置間隔:67
if (k == 0) { // 第一個(gè),特殊處理
// 第一個(gè)點(diǎn),起始點(diǎn)。
[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:下一個(gè)端點(diǎn)的x、y軸坐標(biāo), controlPoint1:X軸坐標(biāo)是上一個(gè)startPoint.x和下一個(gè)endPoint.x的中間X坐標(biāo)位置,Y軸是和上一個(gè)startPoint.y, controlPoint2:X軸坐標(biāo)是上一個(gè)startPoint.x和下一個(gè)endPoint.x的中間X坐標(biāo)位置,Y軸是和下一個(gè)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類轉(zhuǎn)換成CGPath,類似于UIColor的CGColor
minTemp_Layer.path = [weather_MinTempPath CGPath];
[self.top_lineFatherLayer addSublayer:minTemp_Layer];
// 所有的曲線端點(diǎn)(小圓點(diǎn))的位置: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) {
// 端點(diǎn)位置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) {
// 端點(diǎn)位置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]];
}
}