【问题标题】:CABasicAnimation of object around itself (360 degrees)CABasicAnimation 对象自身周围的动画(360 度)
【发布时间】:2014-08-06 23:52:51
【问题描述】:

这是我的动画代码:

  CABasicAnimation *fullRotation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    fullRotation.toValue = [NSNumber numberWithFloat:((360*M_PI)/180)];
    fullRotation.duration = 4;
    fullRotation.repeatCount= 1000;
    [[stick layer] addAnimation:fullRotation forKey:@"60"];

      fullRotation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

我正在尝试围绕自身旋转“棒”,但它似乎围绕另一个点旋转,默认情况下是原点。我该怎么做才能让它完成一个完整的 360 度自转?提前致谢。

【问题讨论】:

    标签: ios objective-c animation rotation cabasicanimation


    【解决方案1】:

    如果你想改变棒使用的锚点:

    stick.layer.anchorPoint = CGPointMake(1.0,1.0);
    

    https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/instp/CALayer/anchorPoint

    我使用的轮换代码:

    CABasicAnimation *rotateAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
        rotateAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        rotateAnim.fromValue = [NSNumber numberWithFloat:0];
        rotateAnim.toValue = [NSNumber numberWithFloat:(360 * M_PI / 180.0f)];
        rotateAnim.repeatCount = HUGE_VALF;
        rotateAnim.duration = 2.5;
        rotateAnim.removedOnCompletion = NO;
    
        [view.layer addAnimation:rotateAnim forKey:@"rotationAnimation"];
    

    【讨论】:

    • "这个属性的默认值是(0.5, 0.5)" 所以基本上如果物体不绕中心旋转,就说明设计有缺陷吧?
    • 因此,如果我想要围绕中心正确旋转,我应该设计对象,使其真正的中心位于画布的中心,并且在 xcode 中我应该放置一个完全相同的 imageview尺寸作为对象设计的画布。对吗?
    • 是的,默认情况下它应该围绕中心旋转,这就是你想要的吗?
    • @Joseph 对,这就是我们过去必须做的事情。
    • 谢谢,我明白了。
    猜你喜欢
    • 2021-02-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多