@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是结束倒计时方法。经过不断的开始和结束,动画的倒计时时长越来越短,是为什么??求大神解惑
if(!_timer){
_timer = [CADisplayLink displayLinkWithTarget:self selector:@selector(circleAnimationType)]; _timer.frameInterval = 1; //设置刷新60次响应一次 [_timer addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
}