代码之家  ›  专栏  ›  技术社区  ›  Smikey

强制顺时针/逆时针旋转以实现UIImageView的cabasicanization

  •  8
  • Smikey  · 技术社区  · 14 年前

    我正在制作一个钟摆的动画,它从0度到最大200度,然后再回来。问题是,如果钟摆超过180度,它会以顺时针方向继续的最短路径返回0。我想逆时针方向。这是我的密码: (‘right’是一个布尔值,当钟摆从左向右摆动时为真)

     - (void)swingPendulum {
        CABasicAnimation *rotationAnimation;
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        if (right) 
            rotationAnimation.toValue = [NSNumber numberWithFloat:degreesToRadians(kMax)];
        else
            rotationAnimation.toValue = [NSNumber numberWithFloat:degreesToRadians(kMin)];                               
        rotationAnimation.duration = 1.0;
        rotationAnimation.repeatCount = 1.0; 
        rotationAnimation.delegate = self;
        rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
        rotationAnimation.removedOnCompletion = NO;
    
        [pendulum.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    }
    

    你知道我该怎么做吗?这是最后一块我的摇摆计拼图,否则是工作得很好:D谢谢!

    4 回复  |  直到 14 年前
        1
  •  8
  •   Bastien Jansen    12 年前

    要让它逆时针转动,只需设置 x 为负值(前面加上) 你想去哪儿

    rotationAnimation.toValue = [NSNumber numberWithFloat:degreesToRadians(x)];
    
        2
  •  2
  •   tc.    14 年前

    尝试设置如下内容

    rotationAnimation.valueFunction = [CAValueFunction functionWithName:kCAValueFunctionRotateZ];
    

    使用从0开始旋转的值变换函数° 至180° 围绕z轴创建一个CAValueTransform函数,指定kCAValueFunctionRotateZ,然后创建一个fromValue为0、toValue为MèPI的动画,并将animations valueTransform属性设置为value transform实例。

        3
  •  1
  •   mipe34 shivbits    12 年前

    我认为你必须使用这个代码:

    [rotationAnimation.byValue = [NSNumber numberWithFloat:degreesToRadians(kMax/2))];
    [rotationAnimation.toValue = [NSNumber numberWithFloat:degreesToRadians(kMin)];
    
        4
  •  0
  •   Matt Long    14 年前

    CABasicAnimation* rotationAnimation = 
              [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    [rotationAnimation setToValue:[NSNumber numberWithFloat:DegreesToRadians(200.0)]];
    [rotationAnimation setDuration:1.0];
    [rotationAnimation setRepeatCount:HUGE_VALF]; // Repeat forever
    [rotationAnimation setAutoreverses:YES]; // Return to starting point
    
    [[pendulum layer] addAnimation:rotationAnimation forKey:nil];