简体   繁体   中英

Spin UIImageView 360 Degrees using UIView animations?

I am simply trying to spin a UIImageView 360 degrees clockwise using this:

#define DEGREES_TO_RADIANS(angle) (angle / 180.0 * M_PI)

and this

imageView.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(360));

However imageView doesn't spin at all even those it is being called. Is there something wrong I am doing? I do not want to use CAAnimations so please don't recommend me to do so.

Thanks!

The problem is that Core Animation will apply animations by finding the most direct route from the current state to the new state. Rotation by 0 degrees is the same as rotation by 360 degrees so the most direct route to your final transform is to do absolutely nothing.

Two steps of 180 degrees would be problematic because there are two equally direct routes from 0 to 180 degrees and Core Animation could pick either. So you probably need to break your animation into three steps. Do the first with a UIViewAnimationOptionCurveEaseIn , the second with UIViewAnimationOptionCurveLinear and the final with UIViewAnimationOptionCurveEaseOut .

Perhaps because the transform isn't happening over a duration of time. It seems to me that as long as it isn't perceivable over time you may need to perform the transform or sequence of transforms over a fraction of time.

Here is the code inspired by Tommy's answer.

I used

NSInteger step;

to keep track of current rotated degree of the image view.

- (void)startAnimation
{
    [UIView animateWithDuration:0.5f 
                          delay:0.0f 
                        options:UIViewAnimationOptionCurveLinear 
                     animations:^{
        imageView.transform = CGAffineTransformMakeRotation(120 * step / 180.0f * M_PI);
    }
                     completion:^(BOOL finished) {
        step++;
        // rotation completed, reset for the next round
        if (step == 4){
            step = 1;
        }

        // perform the next rotation animation
        [self startAnimation];
    }];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM