xcode 是否可以为 UIImage 设置动画?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8545154/
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
Is it possible to animate an UIImage?
提问by Oskar Larsson
I'm wondering if there's anyway to animate an UIImage
.
我想知道是否有任何动画UIImage
.
I know that UIImageViews
are possible to animate but is there any way to do it directly in a UIImage
.
我知道UIImageViews
可以设置动画,但是有什么方法可以直接在UIImage
.
Maybe with Open GL ES or something?
也许使用 Open GL ES 之类的?
Or are there any other ways you can animate an MPMediaItemArtwork
?
或者有没有其他方法可以使MPMediaItemArtwork
?
Thanks in advance!
提前致谢!
回答by richerd
Create a UIImageView
and set the property of animationImages
to an array of UIImage
s
创建 aUIImageView
并将 的属性设置animationImages
为UIImage
s的数组
Here is an example:
下面是一个例子:
NSArray *animationFrames = [NSArray arrayWithObjects:
[UIImage imageWithName:@"image1.png"],
[UIImage imageWithName:@"image2.png"],
nil];
UIImageView *animatedImageView = [[UIImageView alloc] init];
animatedImageView.animationImages = animationsFrame;
[animatedImageView startAnimating];
If you're targeting iOS 5 you can do this directly in UIImage
without the UIImageView
using
如果您的目标是 iOS 5,则可以直接在UIImage
不UIImageView
使用的情况下执行此操作
+(UIImage *)animatedImageWithImages:(NSArray *)images duration:(NSTimeInterval)duration
for example,
例如,
[UIImage animatedImagesWithImages:animationFrames duration:10];
回答by Oskar Larsson
If you mean animation with a few images, use this code:
如果您的意思是带有几张图像的动画,请使用以下代码:
// create the view that will execute our animation
UIImageView* YourImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
// load all the frames of our animation
YourImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"01.gif"],
[UIImage imageNamed:@"02.gif"],
[UIImage imageNamed:@"03.gif"],
[UIImage imageNamed:@"04.gif"],
[UIImage imageNamed:@"05.gif"],
[UIImage imageNamed:@"06.gif"],
[UIImage imageNamed:@"07.gif"],
[UIImage imageNamed:@"08.gif"],
[UIImage imageNamed:@"09.gif"],
[UIImage imageNamed:@"10.gif"],
[UIImage imageNamed:@"11.gif"],
[UIImage imageNamed:@"12.gif"],
[UIImage imageNamed:@"13.gif"],
[UIImage imageNamed:@"14.gif"],
[UIImage imageNamed:@"15.gif"],
[UIImage imageNamed:@"16.gif"],
[UIImage imageNamed:@"17.gif"], nil];
// all frames will execute in 1.75 seconds
YourImageView.animationDuration = 1.75;
// repeat the animation forever
YourImageView.animationRepeatCount = 0;
// start animating
[YourImageView startAnimating];
// add the animation view to the main window
[self.view addSubview:YourImageView];
回答by KPM
Check UIImage's +animatedImageWithImages:duration:
and +animatedImageNamed:duration:
.
检查 UIImage+animatedImageWithImages:duration:
和+animatedImageNamed:duration:
.
From UIImage Class Reference:
Creates and returns an animated image from an existing set of images.
This method loads a series of files by appending a series of numbers to the base file name provided in the name parameter. For example, if the name parameter had ‘image' as its contents, this method would attempt to load images from files with the names ‘image0', ‘image1' and so on all the way up to ‘image1024'. All images included in the animated image should share the same size and scale.
从现有图像集创建并返回动画图像。
此方法通过将一系列数字附加到 name 参数中提供的基本文件名来加载一系列文件。例如,如果 name 参数的内容为“image”,则此方法将尝试从名称为“image0”、“image1”等一直到“image1024”的文件中加载图像。动画图像中包含的所有图像应共享相同的大小和比例。
回答by Fogh
Swift 4 version:
斯威夫特 4 版本:
let animationImages = [UIImage(named: "image1.png"), UIImage(named: "image2.png")]
imageView.animationImages = animationImages
imageView.animationDuration = 10
imageView.startAnimating()
回答by alok
You can use this.
你可以用这个。
arrayOfImages=[[NSMutableArray alloc]init];
for(int i=1;i<=54;i++)
{
NSString *nameResources=@"haKsequence.png";
NSString *changedString= [NSString stringWithFormat:@"%d", i];
NSString *stringWithoutSpaces = [nameResources stringByReplacingOccurrencesOfString:@"K" withString:changedString];
[arrayOfImages addObject:(id)[UIImage imageNamed:stringWithoutSpaces].CGImage];
}
self.view.userInteractionEnabled=NO;
i1.hidden=YES;
image1=[[UIImageView alloc]init];
image1.backgroundColor=[UIColor clearColor];
image1.frame = button.frame;//i1 is obshaped buttion
[self.view addSubview:image1];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
animation.calculationMode = kCAAnimationDiscrete;
animation.duration = 1;
animation.values = arrayOfMonkeyImages;
[animation setDelegate:self];
animation.repeatCount=3;
[animation setRemovedOnCompletion:YES];
[image1.layer addAnimation:animation forKey:@"animation"];