Force clockwise/anticlockwise rotation for a CABasicAnimation of a UIImageView

前端 未结 4 1168
清酒与你
清酒与你 2021-02-15 12:46

I\'m animating a pendulum which swings from 0 degrees to max 200 degrees and then back again. The problem is that if the pendulum goes over 180 degrees, it returns to 0 by the s

4条回答
  •  醉话见心
    2021-02-15 13:34

    I'm not sure exactly what the issue is, but if you use the animation's auto reverse feature, you can probably simplify this and cause it to rotate (swing) back and forth. This works for me:

    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];
    

提交回复
热议问题