3

I have an image that I want to rotate 360° (clockwise) and then repeat and I'm having trouble hitting on the right way to do this. I can do this:

            UIView.Animate(1, 0, UIViewAnimationOptions.Repeat,
            () =>
            {
                LoadingImage.Transform = MonoTouch.CoreGraphics.CGAffineTransform.MakeRotation(-(float)(Math.PI));
            },
            () =>
            {
            });

Which rotates my image 180° and then snaps back to the start and repeats. So, I thought if I do something like 1.99 * PI, it would rotate almost all the way around and probably look ok. Unfortunately, the animation system is smart than that and will just rotate in the opposite direction instead!

So what's the right way to get an image to spin 360° continuously?

Update

With the help or Eric's comment, I've got to this:

        CABasicAnimation rotationAnimation = new CABasicAnimation();
        rotationAnimation.KeyPath = "transform.rotation.z";
        rotationAnimation.To = new NSNumber(Math.PI * 2);
        rotationAnimation.Duration = 1;
        rotationAnimation.Cumulative = true;
        rotationAnimation.RepeatCount = 10;
        LoadingImage.Layer.AddAnimation(rotationAnimation, "rotationAnimation");

Which does spin 360­­°, 10 times in my example (see RepeatCount), but I don't see a way to tell it to repeat until I stop it. CABasicAnimation has a RepeatCount and a RepeatDuration, but doesn't seem to have a property to tell it to just keep repeating. I can set RepeatCount to some suitably high value that it'll keep spinning until the user has probably lost interest, but that doesn't seem like a great solution.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171

3 Answers3

11

Looks like the solution adapted from the link Eric provided looks like the best choice:

    CABasicAnimation rotationAnimation = new CABasicAnimation();
    rotationAnimation.KeyPath = "transform.rotation.z";
    rotationAnimation.To = new NSNumber(Math.PI * 2);
    rotationAnimation.Duration = 1;
    rotationAnimation.Cumulative = true;
    rotationAnimation.RepeatCount = float.MaxValue;
    LoadingImage.Layer.AddAnimation(rotationAnimation, "rotationAnimation");

Set RepeatCount to float.MaxValue to effectively keep it repeating forever (why a count is a float rather than an int remains a mystery).

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
0

Try animation.repeatCount = HUGE_VALF;

koray
  • 242
  • 1
  • 8
0

I think the reason repeat count is a float is to allow for partial cycles of an animation. Maybe you want a circle to animate by pulsing, but stop at the opposite end of where you started.