如何在 iOS 中为视图的移动设置动画?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14130963/
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 I animate the movement of a view in iOS?
提问by user1917407
I'm a very beginner programmer and I'm trying to find the easiest way to do this. I have never done anything with animation before. I want to try to animate an image in my app, and I've run into some problems. This is the code someone previously suggested I use (from a different question):
我是一个非常初级的程序员,我正在尝试找到最简单的方法来做到这一点。我以前从未做过任何动画。我想尝试在我的应用程序中为图像设置动画,但遇到了一些问题。这是之前有人建议我使用的代码(来自另一个问题):
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];
Well, first off, is this even practical? I want to animate something that goes from the middle of the screen to the bottom of the screen. Does that mean I have to have 500 images, each 1 pixel away from each other? What is the optimal pixel interval, so that it looks fluid and even and doesn't require a ton of work? Is there a better way to animate than the way above? Is there a program that helps make animations that you can later add into Xcode?
好吧,首先,这甚至实用吗?我想为从屏幕中间到屏幕底部的东西制作动画。这是否意味着我必须有 500 张图像,每个图像相距 1 个像素?什么是最佳像素间隔,以便它看起来流畅均匀并且不需要大量工作?有没有比上面的方法更好的动画方法?是否有帮助制作动画的程序,您可以稍后将其添加到 Xcode 中?
Also, can someone please explain the code above? I'm new to animation, I don't really understand what all that means.
另外,有人可以解释一下上面的代码吗?我是动画新手,我真的不明白这意味着什么。
Sorry if this is a stupid question, I said at the opening that I am a beginner. Thanks for any help you may provide!
对不起,如果这是一个愚蠢的问题,我在开幕式上说我是初学者。感谢您提供的任何帮助!
回答by MaxGabriel
For most animations, you can simply change the properties of the view inside a UIView
animation block. The UIView
class documentation lists which properties are animatable.
对于大多数动画,您可以简单地更改UIView
动画块内视图的属性。在UIView
类文档列出了哪些属性是动画。
[UIView animateWithDuration:0.5f animations:^{
aView.frame = CGRectOffset(aView.frame, 0, 250);
}];
Here is a sample project demonstrating how you would trigger an animation when a button is pressed. You could put this animation code many other places, so there isn't a good answer to your question about where to put the code unless you give more details about what you want.
这是一个示例项目,演示如何在按下按钮时触发动画。您可以将此动画代码放在许多其他位置,因此除非您提供有关所需内容的更多详细信息,否则关于将代码放在哪里的问题没有很好的答案。
https://github.com/MaxGabriel/AnimationDemonstration
https://github.com/MaxGabriel/AnimationDemonstration
The approach you were taking was animating a UIImageView
. I've never done this, but my understanding is that's like making an animated GIF. For things like moving views or fading them out, you'll want to use the animation block method shown above.
您采用的方法是为UIImageView
. 我从来没有这样做过,但我的理解是这就像制作动画 GIF。对于移动视图或淡出它们之类的事情,您需要使用上面显示的动画块方法。
回答by zeeshan Dar
It is simple to do animation in iOS.
在 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];
回答by Rafael Santos
The following code work for me, to move a view when open a keyboard or other things.
以下代码对我有用,可以在打开键盘或其他东西时移动视图。
[UIView animateWithDuration:(timeYouWantAnimate - 0.3f) animations:^{
[nameOfView setFrame:CGRectMake(0,spaceYouWantMove like -100, self.view.with, self.view.height)];
}
Hope can help you too :D
希望也能帮到你:D