如何在 Xcode 中添加动画?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14129375/
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
How do you add an animation into Xcode?
提问by user1917407
I don't know how to make an animation, or how I would put that into Xcode. Can someone just give me a little background info on this?
我不知道如何制作动画,也不知道如何将其放入 Xcode。有人可以给我一些关于此的背景信息吗?
回答by Makleesh
NSArray *animationArray = [NSArray arrayWithObjects:[UIImage imageNamed@"firstImage",[UIImage imageNamed@"secondImage",nil]; //add as many as you want
Now you need an imageview to set the animation to:
现在你需要一个图像视图来设置动画:
self.myImageView.animationImages =animationArray;
self.myImageView.animationDuration = 3;
self.myImageView.animationRepeatCount = -1; //keeps going infinitely
[self.myImageView startAnimating];
回答by ciara staggs
I am guessing that what you want to do is to create an animated image, rather than animate a view transition... Here is a little background on two ways of creating an animation for iOS.
我猜你想要做的是创建一个动画图像,而不是动画视图转换......这里是关于为 iOS 创建动画的两种方法的一些背景知识。
First option: Create an array of Images (UIImage), then use the startAnimating method. Something like this:
第一个选项:创建一个图像数组(UIImage),然后使用 startAnimating 方法。像这样的东西:
imageView.image = yourLastImage;
// Do this first so that after the animation is complete the image view till show your last image.
NSArray * imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"image1.png"],
[UIImage imageNamed:@"image2.png"],
[UIImage imageNamed:@"image3.png"],
[UIImage imageNamed:@"image4.png"],
nil];
// Note: here you may instead want to use something like [UIImage imageWithContentsOfFile:[self localImagePath:NO]] instead depending upon your targeted iOS version.
UIImageView * animatedImageView = [[UIImageView alloc] initWithFrame:
CGRectMake(100, 125, 150, 130)];
animatedImageView.animationImages = imageArray;
animatedImageView.animationDuration = 1.1;
myAnimation.animationRepeatCount = 1;
animatedImageView.contentMode = UIViewContentModeBottomLeft;
[self.view addSubview:animatedImageView];
[animatedImageView startAnimating];
Second Option: (for iOS 4 and later) You can use block-based methods. Here is a link to a reference article for this here on StackOverflow.
第二个选项:(对于 iOS 4 及更高版本)您可以使用基于块的方法。这是 StackOverflow 上的参考文章的链接。
What are block-based animation methods in iPhone OS 4.0?
Also you may want to take a look at the documentation that Apple provides relating to Core Animation:
此外,您可能还想查看 Apple 提供的与 Core Animation 相关的文档: