如何在Xcode中添加动画?

时间:2013-01-02 21:22:28

标签: ios xcode animation

我不知道如何制作动画,或者我将如何制作动画到Xcode中。有人可以给我一些背景信息吗?

2 个答案:

答案 0 :(得分:5)

NSArray *animationArray = [NSArray arrayWithObjects:[UIImage imageNamed@"firstImage",[UIImage imageNamed@"secondImage",nil];  //add as many as you want

现在您需要一个imageview将动画设置为:

self.myImageView.animationImages =animationArray;
self.myImageView.animationDuration = 3;
self.myImageView.animationRepeatCount = -1; //keeps going infinitely

[self.myImageView startAnimating];

答案 1 :(得分:0)

我猜你想要做的是创建一个动画图像,而不是为视图转换设置动画......这里有两个关于为iOS创建动画的方法的背景知识。

第一个选项:创建一个Images(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];

第二个选项:(适用于iOS 4及更高版本)您可以使用基于块的方法。以下是StackOverflow上此参考文章的链接。

What are block-based animation methods in iPhone OS 4.0?

另外,您可能需要查看Apple提供的与Core Animation相关的文档:

http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Articles/AnimatingLayers.html#//apple_ref/doc/uid/TP40006085-SW1