如何在iOS中为视图的移动设置动画?

use*_*407 10 arrays xcode animation objective-c ios

我是一个非常初学的程序员,我正在努力寻找最简单的方法.我之前从未做过任何关于动画的事情.我想尝试在我的应用程序中为图像设置动画,但我遇到了一些问题.这是我之前建议使用的代码(来自不同的问题):

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];
Run Code Online (Sandbox Code Playgroud)

嗯,首先,这是否实用?我想要动画从屏幕中间到屏幕底部的内容.这是否意味着我必须拥有500张图像,每张图像相距1个像素?什么是最佳像素间隔,使其看起来流畅均匀,不需要大量工作?有没有比上面的方式更好的动画方式?是否有一个程序可以帮助制作动画,以后可以添加到Xcode中?

另外,有人可以解释上面的代码吗?我是动画新手,我真的不明白这一切意味着什么.

对不起,如果这是一个愚蠢的问题,我在开幕式上说我是初学者.感谢您提供的任何帮助!

Max*_*iel 31

对于大多数动画,您只需更改UIView动画块内视图的属性即可.在UIView类文档列出了哪些属性是动画.

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

这是一个示例项目,演示了按下按钮时如何触发动画.您可以将此动画代码放在许多其他位置,因此除非您提供有关所需内容的更多详细信息,否则您对该代码放置位置的问题没有一个好的答案.

https://github.com/MaxGabriel/AnimationDemonstration

你正在采取的方法是动画a UIImageView.我从来没有这样做,但我的理解是,就像制作动画GIF一样.对于移动视图或淡出视图等内容,您需要使用上面显示的动画块方法.

  • 如果你想在完成动画后做一些事情你应该使用这个方法和下一个参数`(void(^)(BOOL completion)`.这里是片段:http://pastebin.com/wfGTrLu9 (2认同)