简体   繁体   English

使用UIView动画旋转UIImageView 360度?

[英]Spin UIImageView 360 Degrees using UIView animations?

I am simply trying to spin a UIImageView 360 degrees clockwise using this: 我只是试图顺时针旋转UIImageView 360度:

#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. 但是,即使调用它们,imageView也根本不会旋转。 Is there something wrong I am doing? 我在做什么不对劲? I do not want to use CAAnimations so please don't recommend me to do so. 我不想使用CAAnimations所以请不要建议我这样做。

Thanks! 谢谢!

The problem is that Core Animation will apply animations by finding the most direct route from the current state to the new state. 问题是Core Animation将通过查找从当前状态到新状态的最直接路由来应用动画。 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. 旋转0度与360度旋转相同,因此最终转换的最直接路径是绝对不做任何事情。

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. 180度的两个步骤将是有问题的,因为有两个同样直接的路线,从0到180度,核心动画可以选择。 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 . 首先使用UIViewAnimationOptionCurveEaseIn ,第二个使用UIViewAnimationOptionCurveLinear ,最后使用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. 以下是Tommy的答案启发的代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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