/**
根据总体偏移比例和相关固定值计算滑块起点坐标x, 滑块长度.
@param sliderWidth 滑块静止时的长度
@param points 滑块静止的站点数,也就是标题数量
@param overallLength 按滑块从第一静止站点的左端到最后静止站点右端的总长度
@param rate 外部输入滑动的比例
@return poin.x 为滑块起点坐标x, point.y 为滑块的长度
*/
- (CGPoint)sliderLocationAndLengthWithSliderWidth:(CGFloat)sliderWidth points:(NSUInteger)points overallLength:(CGFloat)overallLength offsetRate:(CGFloat)rate {
if (rate < 0) {
return CGPointMake(0.0, sliderWidth);
}
if (rate > 1) {
return CGPointMake(overallLength - sliderWidth, sliderWidth);
}
// space 滑块之间的间隔
CGFloat space = (overallLength - points * sliderWidth) / (points - 1);
// 细分滑动段数量, 每个锚点之间分起始段和抵达段两段
NSInteger section = (points - 1) * 2;
// 计算总体的滑动比例rate在细分后的哪一段
NSInteger index = 0;
for (int i = 0; i < section; i++) {
CGFloat max = (CGFloat)(i + 1) / section;
index = i;
if (rate < max) {
break;
}
}
// 判断是起始段还是抵达段
BOOL isStart = (index % 2 == 0);
// 计算总体的滑动比例rate, 在细分段中占该段的比例
CGFloat sectionRate = 1.0 / section;
CGFloat minRate = index * sectionRate;
CGFloat progress = (rate - minRate) / sectionRate;
// 计算起始坐标和对应起始坐标的偏移长度
CGFloat x = 0.0;
CGFloat lenght = 0.0;
if (isStart) {
x = (index / 2) * (sliderWidth + space);
lenght = sliderWidth + (sliderWidth + space) * progress;
}else{
x = (index / 2) * (sliderWidth + space) + (sliderWidth + space) * progress;
lenght = sliderWidth + (sliderWidth + space) * (1 - progress);
}
return CGPointMake(x, lenght);
}
网友评论