简体   繁体   English

UIImageView不会与CGImage的UIImage动画化

[英]UIImageView won't animate with UIImages of CGImage

I have a UIImageView that I am trying to make do an animation of a set of UIImages that are created by flipping other UIImages. 我有一个UIImageView,我试图通过翻转其他UIImage来创建一组UIImage的动画。 Here's my code: 这是我的代码:

    turtle = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width-200,
                                                                    self.view.frame.size.height - sand.frame.size.height - turtle.frame.size.height - 10 - heightOfKeyboard,
                                                                    100,100)];
[flippedTurtleArray addObject:[UIImage imageWithCGImage:[UIImage imageNamed:@"Turtle1.png"].CGImage scale:1 orientation:UIImageOrientationDownMirrored]];
[flippedTurtleArray addObject:[UIImage imageWithCGImage:[UIImage imageNamed:@"Turtle2.png"].CGImage scale:1 orientation:UIImageOrientationDownMirrored]];
[flippedTurtleArray addObject:[UIImage imageWithCGImage:[UIImage imageNamed:@"Turtle3.png"].CGImage scale:1 orientation:UIImageOrientationDownMirrored]];
[flippedTurtleArray addObject:[UIImage imageWithCGImage:[UIImage imageNamed:@"Turtle2.png"].CGImage scale:1 orientation:UIImageOrientationDownMirrored]];
[self.view addSubview: turtle];

Problem is, when I try and make it animate from the array of flipped images, it shows the originals, not the flipped ones (ie, when I do this): 问题是,当我尝试从翻转的图像数组中制作动画时,它显示的是原始图像,而不是翻转的图像(即,当我这样做时):

turtle.animationImages = flippedTurtleArray;
turtle.animationDuration = 0.8f;
turtle.animationRepeatCount = 0;
[turtle startAnimating];

the original non flipped images are shown. 显示原始的非翻转图像。

Now, if I do this however: 现在,如果我这样做:

turtle.image = [flippedTurtleArray objectAtIndex:1];

the flipped image is shown. 将显示翻转的图像。 I thought maybe you can't do the animation with CGImage, but couldn't find that anyone else had had the same problem. 我以为您可能无法使用CGImage进行动画处理,但找不到其他人也遇到过同样的问题。 Any ideas? 有任何想法吗?

Thanks, 谢谢,

Sam 山姆

I would look at the CGLayer property, and specifically the transform property of the layer. 我将研究CGLayer属性,尤其是图层的transform属性。

The Transform property is animatable, and would allow you to apply an arbitrary transform to you original image without having to manage your intermediate flipped Turtle Array. Transform属性是可设置动画的,允许您对原始图像进行任意变换,而不必管理中间的翻转Turtle Array。

The transform applied to the layer's contents. 转换应用于图层的内容。 Animatable. 可动画的。

@property CATransform3D transform Discussion This property is set to the identity transform by default. @property CATransform3D transform讨论默认情况下,此属性设置为Identity转换。 Any transformations you apply to the layer occur relative to the layer's anchor point. 您应用于图层的任何变换都相对于图层的锚点发生。

Availability Available in iOS 2.0 and later. 可用性在iOS 2.0和更高版本中可用。 Related Sample Code oalTouch Declared In CALayer.h CALayer.h中声明的相关示例代码oalTouch

If you have other reasons for wanting the array, then you may want to render the flipped image into a separate image, and pass that into your array. 如果您还有其他需要阵列的原因,则可能需要将翻转的图像渲染为单独的图像,然后将其传递到阵列中。

Similarly if you can use iOS 7 as a target, take a look a SpriteKit, it may make the whole task much simpler. 同样,如果您可以使用iOS 7作为目标,请查看SpriteKit,它可以使整个任务更加简单。

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

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