使用CABasicAnimation时不准确的旋转...与正确的角度不匹配

时间:2012-11-22 19:59:16

标签: rotation core-animation cabasicanimation

我正在开发一个地图应用程序。我在地图中绘制一个动画线,然后使用一个应该在线上绘制的图像(标尺图像)。要画线,我使用带路径的CABasicAnimation:



    -(void)lineaAnimation {
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
        animation.duration = animDuration;
        animation.delegate=self;
        animation.repeatCount = 0;
        animation.autoreverses = NO;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.fromValue = (id)inip;
        animation.toValue = (id)finp;
        animation.fillMode=kCAFillModeForwards;
        animation.removedOnCompletion=NO;
        [shapeLayer addAnimation:animation forKey:@"animatePath"];
    }

    -(void)linea:(CGPoint)origen to:(CGPoint)destino animado:(BOOL)anim duracion:(float)durac inicio:(float)delay {
        inip    = CGPathCreateMutable();
        finp    = CGPathCreateMutable();

        CGPathMoveToPoint(inip, nil, origen.x, origen.y);
        CGPathAddLineToPoint(inip, nil, origen.x, origen.y);
        CGPathCloseSubpath(inip);

        CGPathMoveToPoint(finp, nil, origen.x, origen.y);
        CGPathAddLineToPoint(finp, nil, destino.x, destino.y);
        CGPathCloseSubpath(finp);

        rootLayer   = [CALayer layer];
        rootLayer.frame = fondo.bounds;
        [fondo.layer addSublayer:rootLayer];

        shapeLayer = [CAShapeLayer layer];
        shapeLayer.path = inip;
        UIColor *fillColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7];
        shapeLayer.fillColor = fillColor.CGColor;
        UIColor *strokeColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.7];
        shapeLayer.strokeColor = strokeColor.CGColor;
        shapeLayer.lineWidth = 5.0;
        shapeLayer.fillRule = kCAFillRuleEvenOdd;
        [morrallaLayer addObject:shapeLayer];
        [rootLayer addSublayer:shapeLayer];
        animDuration=durac;
        [self performSelector:@selector(lineaAnimation) withObject:nil afterDelay:delay];
    }

然后我尝试将规则的中心移动到该点并使用以下代码旋转标尺图像:



    delta=acos((destino.y-origen.y)/(destino.x-origen.x));
    giroIni=0.0;
    giroFin=delta;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    animation.fromValue= [NSNumber numberWithFloat:giroIni];
    animation.toValue= [NSNumber numberWithFloat:giroFin];
    animation.delegate = self;
    animation.repeatCount = 0;
    animation.autoreverses = NO;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.duration = animDuration;
    animation.fillMode=kCAFillModeForwards;
    animation.removedOnCompletion=NO;
    [ruleL addAnimation:animation forKey:@"giroAngulo"];

问题是标尺边界与线条绘制不匹配,根据最终方向存在一定程度的差异......为什么? 非常感谢!

1 个答案:

答案 0 :(得分:0)

Ooooops ...抱歉你的时间:( 我发现了问题并且......像往常一样,错误不在星星中,而在于我们自己

当我显然应该使用atan

时,我会使用acos功能