两个不同layer的不同动画不同步问题

今天在写一个路径动画的时候发现 划线的速度和呼吸灯的速度不一致,让我一直摸不清问题。。饶了很多的弯路。。
关键就是layer 的timeFunc属性不同步

下面把一部分核心代码贴出来。。

NSLog(@"动画开始执行的时间%@",[[NSDate date] stringWithDateFormat:@"hh:mm:ss:SSS"]);
        self.pathLayer.frame = self.container.bounds;
         LPChartsModel * model = self.dataArray.lastObject;
        CGPoint lastTwoPoint = [self getLastTwoModelPostion];
        //倒数第一
        CGPoint nextPoint  = [self getLastModelPostion];
        self.pulsingLayer.position =  lastTwoPoint;
        //新的最新点
        UIBezierPath * path = [UIBezierPath bezierPath];
        [path moveToPoint:lastTwoPoint];
        [path addLineToPoint:nextPoint];
        self.pathLayer.path = path.CGPath;
        self.pathLayer.strokeColor = [MainPurpleColor CGColor];
        self.pathLayer.lineWidth = 1;
        self.pathLayer.strokeEnd = 0;
        [self.container.layer addSublayer:_pathLayer];
        
        //设置动画启动时间
        CFTimeInterval currentTime = CACurrentMediaTime();
//        CFTimeInterval beginTime = [self.pathLayer convertTime:currentTime fromLayer:self.pulsingLayer];
        //设置动画时长
        CFTimeInterval animationDuration = 0.800;
//        CFTimeInterval duration = [self.pathLayer convertTime:animationDuration fromLayer:self.pulsingLayer];
        POPBasicAnimation *pathAnimation = [POPBasicAnimation animationWithPropertyNamed:kPOPShapeLayerStrokeEnd];
        pathAnimation.removedOnCompletion = NO;
        pathAnimation.fromValue = [NSNumber numberWithFloat:0.0];
        pathAnimation.toValue = [NSNumber numberWithFloat:1.0];
        pathAnimation.autoreverses = NO;
        pathAnimation.beginTime = currentTime;
        pathAnimation.repeatCount = 1;
        pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        pathAnimation.duration = animationDuration;
        CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        //在动画设置一些变量
        positionAnimation.calculationMode = kCAAnimationPaced;
        //我们希望动画持续
        //如果我们动画从左到右的东西——我们想要呆在新位置,
        //然后我们需要这些参数
        positionAnimation.removedOnCompletion = NO;
        positionAnimation.fillMode = kCAFillModeForwards;
        positionAnimation.duration = animationDuration;//完成动画的时间
        //让循环连续演示
        positionAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        positionAnimation.beginTime = currentTime;
        positionAnimation.repeatCount = 1;
        positionAnimation.path = self.pathLayer.path;
        positionAnimation.delegate = self;
        positionAnimation.autoreverses = NO;
        positionAnimation.removedOnCompletion = NO;
        UIBezierPath * newPath = [UIBezierPath bezierPath];
        [newPath moveToPoint:CGPointMake(self.leftMargin,nextPoint.y)];
        [newPath addLineToPoint:CGPointMake(self.maxWidth - self.rightMargin, nextPoint.y)];
        CABasicAnimation * yAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
        yAnimation.repeatCount = 1;
        yAnimation.removedOnCompletion = NO;
        yAnimation.fillMode = kCAFillModeForwards;
        yAnimation.fromValue = (__bridge id)self.currentPriceLayer.path;
        yAnimation.toValue = (__bridge id)newPath.CGPath;
        yAnimation.autoreverses = NO;
        
        CABasicAnimation * CurrentPriceYAnimation = [CABasicAnimation animationWithKeyPath:@"position.y"];
        CurrentPriceYAnimation.repeatCount = 1;
        CurrentPriceYAnimation.removedOnCompletion = NO;
        CurrentPriceYAnimation.fillMode = kCAFillModeForwards;
        CurrentPriceYAnimation.autoreverses = NO;
        CurrentPriceYAnimation.fromValue = [NSNumber numberWithFloat:self.priceLayer.position.y];
        CurrentPriceYAnimation.toValue = [NSNumber numberWithFloat:nextPoint.y];
        [self checkWithModel:model];
        [self.pulsingLayer addAnimation:positionAnimation forKey:@"position"];
        [self.currentPriceLayer addAnimation:yAnimation forKey:@"current"];
        [self.pathLayer pop_addAnimation:pathAnimation forKey:@"strockend"];
        self.drawAnimate = NO;

你可能感兴趣的:(两个不同layer的不同动画不同步问题)