我该如何在iOS中实现视图动画?

10

我是一个非常初级的程序员,正在尝试找到最简单的方法来实现这一点。我以前从未涉及过动画。我想尝试在我的应用程序中对图像进行动画处理,但遇到了一些问题。这是有人先前建议我使用的代码(来自另一个问题):

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];

首先,这是否实际可行?我想要制作一个从屏幕中间到底部移动的动画。这是否意味着我必须有500张图片,每张图片之间相距1像素?什么是最佳像素间隔,使其看起来流畅均匀且不需要大量工作?有比上述方法更好的动画方式吗?是否有可以帮助制作动画并将其添加到Xcode中的程序?

此外,有人能解释一下上面的代码吗?我是新手,真的不太明白所有那些东西是什么意思。

如果这是一个愚蠢的问题,我对此感到抱歉。我在开头说过我是初学者。谢谢您所提供的任何帮助!

3个回答

33

对于大多数动画,您可以在UIView动画块中更改视图的属性。 UIView类文档列出了可进行动画处理的属性。

[UIView animateWithDuration:0.5f animations:^{
    aView.frame = CGRectOffset(aView.frame, 0, 250); 
}];

以下是一个示例项目,演示了如何在按下按钮时触发动画。您可以将此动画代码放在许多其他地方,因此,如果您提供更多细节,您的问题关于在哪里放置代码没有一个好的答案。

https://github.com/MaxGabriel/AnimationDemonstration

你之前采用的方法是对 UIImageView 进行动画处理。我从未尝试过这样做,但我的理解是这就像制作动态图一样。对于移动视图或使其淡出等操作,您应该使用上面显示的动画块方法。


2
如果您想在完成动画后执行其他操作,您应该使用带有下一个参数 (void(^)(BOOL completion) 的此方法。这是代码片段:http://pastebin.com/wfGTrLu9 - Tomasz Szulc
这个应该放在main.m、appName.h还是appName.m中?再说一遍,我是个初学者。 - adamcircle
@user1917407 我应该指出,我在示例项目中只是在动画化一个标准的 UIView。如果您想要移动一张图片,您需要将图片放置在一个 UIImageView 中,然后像示例项目中移动 UIView 一样移动 UIImageView - MaxGabriel
@MaxGabriel 是的,但是什么/在哪里是UIViewController子类?那到底是什么? - adamcircle
@user1917407,“UIViewController”是模型-视图-控制器架构模式中的控制器。视图控制器负责管理应用程序的每个“页面”。当您使用iOS模板创建Xcode项目时,它会带有一个UIViewController子类作为根视图控制器。请参见链接的Github项目以获取示例。这是您需要了解的关于iOS最基本的知识--我强烈建议在学习动画之前先去http://www.stanford.edu/class/cs193p/cgi-bin/drupal/学习iOS基础知识。 - MaxGabriel
显示剩余3条评论

1

在iOS中进行动画很简单。

CGRect basketTopFrame = self.upperview.frame;
basketTopFrame.origin.y = -basketTopFrame.size.height;

CGRect basketBottomFrame = self.lowerview.frame;
basketBottomFrame.origin.y = self.view.bounds.size.height;

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelay:1.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

self.upperview.frame = basketTopFrame;
self.lowerview.frame = basketBottomFrame;

[UIView commitAnimations];

Zeeshan,我在ViewController.m中尝试了你的代码作为一个方法,并且在ViewController.h中我也设置了@property (nonatomic, strong) UIView *upperview; @property (nonatomic, strong) UIView *lowerview;。我该如何测试它?我应该看到什么? - Greg
它会显示前后图像,你需要在这个过程中有三个图像,第一个和第二个图像在动画后隐藏,第三个图像在动画后显示。谢谢。 - Muhammad Zeshan Mazhar

0
以下代码对我有效,可以在打开键盘或其他东西时移动视图。
[UIView animateWithDuration:(timeYouWantAnimate - 0.3f) animations:^{
 [nameOfView setFrame:CGRectMake(0,spaceYouWantMove like -100, self.view.with, self.view.height)];
}

希望也能对你有所帮助 :D


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接