[iOS]竖向时间控制刻度条,支持双指缩放。

时间:2021-7-20 作者:qvyue

原理:依赖scrollview的zoom功能来实现双指缩放,获取缩放参数,同时再对刻度进行重绘。

先上demo:https://github.com/AlbatrossCoder/VerticalTimeRuler

效果图:

[iOS]竖向时间控制刻度条,支持双指缩放。
Simulator Screen Shot – iPhone 8 – 2021-04-12 at 16.29.33.png
[iOS]竖向时间控制刻度条,支持双指缩放。
Simulator Screen Shot – iPhone 8 – 2021-04-12 at 16.29.16.png
[iOS]竖向时间控制刻度条,支持双指缩放。
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

缩放

图层为上层接收缩放,下层展示刻度
上层需实现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  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  30){
            distanceValue = 10;
            rulerAverage = [NSNumber numberWithInt:1];
        }else if(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  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看代码。

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:qvyue@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。