Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I animate the movement of a view in iOS?

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?

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!

like image 682
adamcircle Avatar asked Jan 02 '13 23:01

adamcircle


People also ask

How do you animate a view in iOS Swift?

To be exact, whenever you want to animate the view, you actually call layoutIfNeeded on the superview of that view. Try this instead: UIView. animate(withDuration: 0.1, delay: 0.1, options: UIViewAnimationOptions.


2 Answers

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

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.

like image 181
MaxGabriel Avatar answered Sep 30 '22 14:09

MaxGabriel


It is simple to do animation in 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];
like image 39
Muhammad Zeeshan Avatar answered Sep 30 '22 13:09

Muhammad Zeeshan