ios - CAShapeLayer设计了一个倒计时动画 在不断开始和结束过程中越走越快

 书友73277355 发布于 2022-10-26 13:28
@property(nonatomic,strong)CADisplayLink *timer;//定时器

#pragma mark - lazyInstall
-(CAShapeLayer*)shapeLayer {
    if (!_shapeLayer) {
        //创建出CAShapeLayer
        self.shapeLayer = [CAShapeLayer layer];
        self.shapeLayer.frame = CGRectMake(0, 0, 57, 80);
        self.shapeLayer.position = self.potrait.center;
        self.shapeLayer.fillColor = [UIColor clearColor].CGColor;
        
        //设置线条的宽度和颜色
        self.shapeLayer.lineWidth = 2.0f;
        self.shapeLayer.strokeColor = [UIColor greenColor].CGColor;
       
        //创建出圆形贝塞尔曲线
        UIBezierPath *circlePath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 57, 80) cornerRadius:3];
        //让贝塞尔曲线与CAShapeLayer产生联系
        self.shapeLayer.path = circlePath.CGPath;
    }
    return _shapeLayer;
}

#pragma mark - Timer
-(void)startCycle {//用定时器模拟数值输入的情况
    [self.layer addSublayer:self.shapeLayer];
    
    _shapeLayer.strokeStart = 0;
    _shapeLayer.strokeEnd = 1;

    _timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(circleAnimationType)];
    _timer.frameInterval = 1; //设置刷新60次响应一次
    [_timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)circleAnimationType {//用定时器调用的方法
    if (_shapeLayer.strokeStart != 1) {
        _shapeLayer.strokeStart += 倒计时时间;
    }
}

-(void)endCycle {
    [_timer invalidate];
    _timer = nil;
    
    _shapeLayer.strokeStart = 1;
    _shapeLayer.strokeEnd = 0;
//    [_timer setFireDate:[NSDate distantFuture]];
}

其中-(void)startCycle是开始倒计时方法,-(void)endCycle是结束倒计时方法。经过不断的开始和结束,动画的倒计时时长越来越短,是为什么??求大神解惑

1 个回答
  • if(!_timer){

     _timer = [CADisplayLink displayLinkWithTarget:self         selector:@selector(circleAnimationType)];
    _timer.frameInterval = 1; //设置刷新60次响应一次
    [_timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    }

    2022-10-27 00:51 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有