xcode 使用核心动画旋转 UIImageView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5636570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:48:41  来源:igfitidea点击:

spin UIImageView with core animation

objective-cxcodeipadios4

提问by Xana

I am trying to make something relatively simple, make a wheel spin 90degrees when the user touches a button, for the most part what I have done so far works the first time, but thereafter the image won't move. I want it to keep its new position and rotate 90degrees thereafter from the new position, I have [UIView setAnimationBeginsFromCurrentState:YES];

我正在尝试做一些相对简单的事情,当用户触摸按钮时使轮子旋转 90 度,到目前为止我所做的大部分工作都是第一次起作用,但此后图像不会移动。我希望它保持它的新位置,然后从新位置旋转 90 度,我有 [UIView setAnimationBeginsFromCurrentState:YES];

but that doesn't seem to work

但这似乎不起作用

I added an NSTimer to "reset" the original image orientation, so that the wheel springs back to the original position and then rotates again when the user touches it. ... however this isn't really what I want.

我添加了一个 NSTimer 来“重置”原始图像方向,以便轮子弹回原始位置,然后在用户触摸它时再次旋转。……然而这并不是我真正想要的。

Is there a way to make the image keep rotating 90degrees so four touches by the user would put the image back to its original position. Thanks for any help!!

有没有办法让图像保持旋转 90 度,这样用户的四次触摸就会将图像放回原来的位置。谢谢你的帮助!!

here is the sample code

这是示例代码


-(IBAction) rotatewheel:(id)sender {

      [UIView setAnimationDuration:1];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationRepeatCount:1];
    wheel.transform = CGAffineTransformMakeRotation(M_PI/4);
    [UIView commitAnimations];  

    returnwheelTimer = [NSTimer scheduledTimerWithTimeInterval:1.1 target:self selector:@selector (returnwheel) userInfo:nil repeats:NO];

}

-(void) returnwheel {

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDuration:.5];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationRepeatCount:1];
    wheel.transform = CGAffineTransformMakeRotation(M_PI);
    [UIView commitAnimations];  

}

回答by Jan Gressmann

Instead of:

代替:

wheel.transform = CGAffineTransformMakeRotation(M_PI/4);

use

wheel.transform = CGAffineTransformRotate(wheel.transform,M_PI/4);

That way it will apply the new tranformation to the current one, instead of replacing it.

这样,它会将新的转换应用于当前的转换,而不是替换它。

回答by Max von Hippel

There are three easy ways to implement this.
1) Make your app in a game engine, such as Unity3D, rather than in XCode. Make an animation of the spinning wheel and script it to start on the button push. This is relatively simple to do and there are many good existing tutorials. I recommend using iGUI by Avaam Studios (I am not affiliated with them or biased in any way).

有三种简单的方法可以实现这一点。
1) 在游戏引擎中制作您的应用程序,例如 Unity3D,而不是在 XCode 中。制作旋转轮的动画并编写脚本以在按钮按下时开始。这做起来相对简单,并且有很多很好的现有教程。我建议使用 Avaam Studios 的 iGUI(我不隶属于他们或以任何方式有偏见)。

2) Create an NSObject with an array of photos and start a stop motion animation of the wheel spinning on the button push. A thread on this subject can be found here.

2) 创建一个包含一系列照片的 NSObject 并开始一个按按钮旋转的轮子的定格动画。可以在此处找到有关此主题的线程。

3) If you want the wheel to spin 90 degrees FROM its last location, so that it can be spun full circle (36) after four clicks, then a different approach is needed. I recommend making the button be replaced with a different, identical one each time it is pushed. That way you do not need to do the math on the number of pushes. You would have four different IBOutlets (one for each identical button) and each button would a) turn the image 90 degrees, then b) disappear and be replaced with another button. I have not seen any practical implementations of this but it is feasible. Hereis a thread on the subject of multiple methods on one button (possibly a better alternative to my idea).

3) 如果您希望轮子从其上一个位置旋转 90 度,以便在单击四次后可以旋转整圈 (36),则需要采用不同的方法。我建议每次按下按钮时,将按钮替换为不同的、相同的按钮。这样你就不需要对推送次数进行数学计算。您将有四个不同的 IBOutlets(每个相同的按钮一个),每个按钮都会 a) 将图像旋转 90 度,然后 b) 消失并被另一个按钮替换。我还没有看到任何实际的实现,但它是可行的。 是关于一个按钮上多种方法的主题的主题(可能是我想法的更好替代方案)。