[iOS]竖向时间控制刻度条,支持双指缩放。
2021-04-12 本文已影响0人
世上无南西
原理:依赖scrollview的zoom功能来实现双指缩放,获取缩放参数,同时再对刻度进行重绘。
先上demo:https://github.com/AlbatrossCoder/VerticalTimeRuler
效果图:
Simulator Screen Shot - iPhone 8 - 2021-04-12 at 16.29.33.png Simulator Screen Shot - iPhone 8 - 2021-04-12 at 16.29.16.png Simulator Screen Shot - iPhone 8 - 2021-04-12 at 16.56.37.png
同时支持刻度值展示,调用如下方法即可:
// 传入成对的value起止值
-(void)setRulerColorValue:(NSArray *)data;
刻度
有横向小格子,纵向灰色线,纵向着色线,使用CGPath画线
以着色举例:
shapeLayer3 = [CAShapeLayer layer];
shapeLayer3.strokeColor = [UIColor colorWithRed:22/255.0 green:148/255.0 blue:156/255.0 alpha:1].CGColor;
shapeLayer3.fillColor = [UIColor clearColor].CGColor;
shapeLayer3.lineWidth = 4.f;
shapeLayer3.lineCap = kCALineCapButt;
for (int i = 0; i<data.count; i++) {
// 取值
NSDictionary * dic = data[i];
NSNumber * startPoint = dic[@"startTime"];
NSNumber * stopPoint = dic[@"stopTime"];
CGFloat startY = _distanceValue * (startPoint.floatValue / [self.rulerAverage floatValue]) + 20;
CGFloat endY = _distanceValue * (stopPoint.floatValue / [self.rulerAverage floatValue]) + 20;
//绘线
CGPathMoveToPoint(pathRef3, NULL, self.rulerWidth - 2, startY );//起点
CGPathAddLineToPoint(pathRef3, NULL, self.rulerWidth - 2, endY);//终点
}
shapeLayer3.path = pathRef3;// 着色
[self.layer addSublayer:shapeLayer3];
[self.layer layoutIfNeeded];
缩放
图层为上层接收缩放,下层展示刻度
上层需实现zoom功能:
self.maskScrollView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
self.maskScrollView.minimumZoomScale = 1;
self.maskScrollView.maximumZoomScale = 10;
self.maskScrollView.bouncesZoom = YES;
fakeView = [[UIView alloc]initWithFrame:self.maskScrollView.bounds];
[self.maskScrollView addSubview:fakeView];
self.maskScrollView.zoomScale = 5;
zoom代理设置:
-(void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
if(scrollView == _rulerScrollView) return;
NSLog(@"scrollViewWillBeginZooming:%ld withView:%ld",scrollView.tag, view.tag);
isZooming = YES;
}
-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
if(scrollView == _rulerScrollView) return;
distanceValue = distanceValue * ((scrollView.zoomScale + 15)/20.0);
NSLog(@"distanceValue:%.2f",distanceValue);
if(rulerAverage.intValue == 12 && distanceValue < 6){
distanceValue = 6;
return;
}else if (rulerAverage.intValue == 1 && distanceValue > 30){
distanceValue = 30;
return;
}
if (rulerAverage.intValue == 12){
if(distanceValue > 20){
distanceValue = 10;
rulerAverage = [NSNumber numberWithInt:6];
}
}else if(rulerAverage.intValue == 6){
if(distanceValue > 20){
distanceValue = 10;
rulerAverage = [NSNumber numberWithInt:3];
}else if (distanceValue < 5){
distanceValue = 10;
rulerAverage = [NSNumber numberWithInt:12];
}
}else if (rulerAverage.intValue == 3){
if(distanceValue > 30){
distanceValue = 10;
rulerAverage = [NSNumber numberWithInt:1];
}else if(distanceValue < 5){
distanceValue = 10;
rulerAverage = [NSNumber numberWithInt:6];
}
}else if (rulerAverage.intValue == 1){
if(distanceValue < 10){
distanceValue = 10;
rulerAverage = [NSNumber numberWithInt:3];
}
}
if(!shoudChange && distanceValue > 0){
NSLog(@"rulerAverage:%d distanceValue:%.2f",rulerAverage.intValue, distanceValue);
[self drawRuler];
[_rulerScrollView drawColorRuler:_valueArr];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self->shoudChange = NO;
});
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
if(scrollView == _rulerScrollView) return;
NSLog(@"scrollViewDidEndZooming:%ld withView:%ld scale:%f",scrollView.tag, view.tag, scale);
self.maskScrollView.zoomScale = 5;
self.maskScrollView.contentSize = CGSizeMake(0, self.rulerScrollView.contentSize.height+20);
self.maskScrollView.contentOffset = self.rulerScrollView.contentOffset;
isZooming = NO;
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return fakeView;
}
注意上层ScrollView中有个透明的view ,fakeVew,这个必须在ScrollView实现zoom功能之前放进ScrollView中,zoom才会生效。
处理两个ScrollView的联动及滚动:
- (void)scrollViewDidScroll:(BDRulerScrollView *)scrollView {
if(scrollView == self.rulerScrollView){
CGFloat offSetY = scrollView.contentOffset.y;
CGFloat ruleValue = (offSetY / distanceValue) * [scrollView.rulerAverage floatValue];
if (ruleValue < 0.f) {
return;
} else if (ruleValue > scrollView.rulerCount * [scrollView.rulerAverage floatValue]) {
return;
}
if (self.rulerDeletate) {
if(!isZooming && !isInit){
defaultValue = ruleValue;
}
[self.rulerDeletate txhRrettyRuler:scrollView withValueOfRuler:ruleValue];
}
}else{
if(isInit){
isInit = NO;
}else{
if(isZooming){
return;
}
self.rulerScrollView.contentOffset = CGPointMake(0, self.maskScrollView.contentOffset.y);
}
}
}
- (void)scrollViewDidEndDecelerating:(BDRulerScrollView *)scrollView {
if(self.rulerScrollView == scrollView){
if([self.rulerDeletate respondsToSelector:@selector(moveActionFinish)]){
[self.rulerDeletate moveActionFinish];
}
}
}
- (void)scrollViewDidEndDragging:(BDRulerScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if(!decelerate){
[self scrollViewDidEndDecelerating:scrollView];
}else{
[scrollView setContentOffset:scrollView.contentOffset animated:NO];
}
}
PS:总的说来实现还是比较简单,但是做这个东西的时候时间紧迫写帖子的时候也就还没来得及写备注,不过代码应该很好读懂,建议直接下demo看代码。