ios 同时动画旋转/缩放/平移 UIImage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9622125/
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-08-30 17:14:06  来源:igfitidea点击:

Animate Rotation/Scale/Translate of UIImage at the same time

objective-ciosanimationrotationtranslation

提问by headkit

I am new to objective-c and I want to add an image to the screen, tweening it like in AS3, moving it from one end to the other of the screen while rotating around its own center point.

我是objective-c的新手,我想在屏幕上添加一个图像,像在AS3中一样对它进行补间,将它从屏幕的一端移动到另一端,同时围绕它自己的中心点旋转。

I tried with

我试过

[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            // TRANSFORM SCREENSHOT
            screenShotView.transform = CGAffineTransformRotate(screenShotView.transform, -M_PI * 0.05);
            screenShotView.transform = CGAffineTransformScale(screenShotView.transform, 0.6, 0.6);
            screenShotView.transform = CGAffineTransformTranslate(screenShotView.transform, 
                                                                  self.webView.frame.origin.x,
                                                                  self.webView.frame.origin.y - self.webView.frame.size.height * 0.3
                                        );

but with this code the image rotates around the center of the TransformIdentity. So while rotating and moving the rotation gets out of controll and the image isn't exactly at the position I loved it to be.

但是使用此代码,图像会围绕 TransformIdentity 的中心旋转。因此,在旋转和移动时,旋转会失控,图像并不完全处于我喜欢的位置。

What is the right way to rotate and translate at the same time, translating the rotation center with the image?

同时旋转和平移,将旋转中心与图像一起平移的正确方法是什么?

and at least after transformation I want to add a close button to the right upper corner of the image. for that I need the new coordinates of the corner, too.

至少在转换后我想在图像的右上角添加一个关闭按钮。为此,我也需要角落的新坐标。

thnx!

谢谢!

回答by headkit

I now ended with the following code, but I still don't know if this is the state of the art solution.

我现在以以下代码结束,但我仍然不知道这是否是最先进的解决方案。

CGAffineTransform translate = CGAffineTransformMakeTranslation(self.webView.frame.origin.x,self.webView.frame.origin.y - self.webView.frame.size.height * 0.25);
CGAffineTransform scale = CGAffineTransformMakeScale(0.6, 0.6);
CGAffineTransform transform =  CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformRotate(transform, degreesToRadians(-10));

[UIView beginAnimations:@"MoveAndRotateAnimation" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    [UIView setAnimationDuration:2.0];

    screenShotView.transform = transform;

[UIView commitAnimations];

回答by Ronny Webers

IOS 7's preferred way to do this would be using a block object. It has several advantages compared to the 'older' way of animating. Especially that it can make use of multi-core and video co-processing. Also the 'built-in' call back part (the completion part) is very useful, as it keeps any necessary state information and links to objects as needed.

IOS 7 的首选方法是使用块对象。与“旧”的动画制作方式相比,它有几个优点。特别是它可以利用多核和视频协处理。此外,“内置”回调部分(完成部分)非常有用,因为它根据需要保留了任何必要的状态信息和到对象的链接。

CGAffineTransform translate = CGAffineTransformMakeTranslation(self.webView.frame.origin.x,self.webView.frame.origin.y - self.webView.frame.size.height * 0.25);
CGAffineTransform scale = CGAffineTransformMakeScale(0.6, 0.6);
CGAffineTransform transform =  CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformRotate(transform, degreesToRadians(-10));

// animation using block code
[UIView animateWithDuration:2.0
                      delay:0.0
                    options:UIViewAnimationOptionCurveEaseIn
                 animations:^{
                     screenShotView.transform = transform;
                 }completion:^(BOOL finished){
                     // do something if needed
                 }];