【问题标题】:Inaccurate rotation with CABasicAnimation... doesn't match the correct angleCABasicAnimation 的旋转不准确...与正确的角度不匹配
【发布时间】:2012-11-22 19:59:16
【问题描述】:

我正在开发一个地图应用程序。我在地图上画了一条动画线,然后使用应该在这条线上绘制的图像(标尺图像)。为了画线,我使用带有路径的 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"];

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

【问题讨论】:

    标签: rotation core-animation cabasicanimation


    【解决方案1】:

    哎呀......对不起你的时间:( 我发现了问题,并且.. 像往常一样,错误不在星星而是在我们自己

    当我显然应该使用 atan 时,我使用 acos 函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2017-08-19
      • 1970-01-01
      • 2021-08-30
      • 2019-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多