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

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!

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.

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

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

Related

Moving a UIView

I am trying to move a UIView programmatically using the following method:
-(void)animatePlaybackLine: (int) currentBeat{
CGRect newFrame = CGRectMake(currentBeat*eighthNoteWidth, 0, eighthNoteWidth, 320);
[playbackLine setFrame:newFrame];
NSLog(#"Line animated to %i", currentBeat);
NSLog(#"New frame origin %f", playbackLine.frame.origin.x);
}
When the method is called, the NSLogs show that the variable currentBeat is incrementing as it should, and the frame of playbackLine (my UIView) appears to be moving as it should. However, the object on the screen doesn't move. I have also tried setting the bounds and the center instead of the frame, and all of them have similar results. I also tried using an animation instead of setting the frame, to no avail.
Is there something else I should be doing to make the image onscreen show the changing frame?
I'm guessing you're calling that method in a loop? iOS isn't going to redraw the screen until your code finishes executing. You can't loop like this and see iterative results.
To animate views, you should use the native support UIKit gives you. In particular, check out the Animations section in the UIView class reference.
Try this
[UIView animateWithDuration:0.3
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{
datePickerView.frame = imageFrame;
}
completion:^(BOOL finished){
self.datePickerViewFlag = NO;
}];

How to set growup effect to play video inside that view?

I want to create a application in iPad. like "Showyou" in iTunes
For that i need to set growup effect of the view when user touch the video thumnail image and hide with animation at its place when user touch outside the view like popover in ipad.
I haven't used the ShowYou app but it seems like you need an animation to grow/shrink a view and possibly move it. If that's the case try this
// to make it grow in this case 1.5 and move it to a into position in this case 100, 100
[UIView animateWithDuration:_animationDuration
animations:^{
CGAffineTransform transform = CGAffineTransformMakeScale(1.5f, 1.5f);
testView.transform = CGAffineTransformTranslate(transform, 100.0f, 100.0f);
}
];
// make it go back
[UIView animateWithDuration:_animationDuration
animations:^{
someView.transform = CGAffineTransformIdentity;
}
];
You might want to use animateWithDuration:delay:options:animations:completion: so that you can add code to do something after the animation is done. Hopefully that's what you are looking for. :)

How do you change the size of of an image when you push a button in Objective C - xcode?

I am trying to change the size of an image when a button is pushed to make it get longer or shorter. I would prefer it if it could be animated into changing its size, but without animation would also help, if anyone can give me any information if this is possible, and how then it would be appreciated
Thanks for your help in advance
Assuming you are using a UIImageView then you simply change the size (this may cause bad effects depending on how much you stretch etc).
NOTE: newFrame is whatever your new frame will be
- (void)buttonTapped:(id)sender
{
CGRect newFrame = ...;
self.myImageView.frame = newFrame;
}
To animate it stick this inside an animation block
- (void)buttonTapped:(id)sender
{
CGRect newFrame = ...;
[UIView animateWithDuration:0.25f
animations:^{
self.myImageView.frame = newFrame;
}];
}

How to implement the effect of popping up tableview from bottom of screen?

I'm a newbie of Xcode and Objective-c. Recently I wanna do a task about popping up tableviews. When a button is pressed, the tableview should pop up from the bottom of the screen. The tableview will contain just a few items so I don't wanna it occupy full screen. I read the manual and found UIModalPresentationStyle, but it seems not fulfill my requirements. So what's the more accurate methods I can use?
While #Bharat J's answer is correct, those methods are deprecated in iOS 4.0.
The latest and greatest implementation is + (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
Implemented as:
[UIView animateWithDuration:0.5 animations: ^{
tableView.frame = newFrame;
}
];
You can use below code to create an animation block . Set the frame for the table view as (0,480,320,0) When you hit a button change the frame of the table view in the animation block and make it to CGRectMake(0,200,320,280) or something .
[UIView beginAnimations:#"AnimateTableView" context:view];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
tableView.frame = CGRectMake(0, 200 , 320, 280);
[UIView commitAnimations];
The same animation block for hiding it again but with the frame again begin set to CGRectMake(0,480,320,0). This should work in your case .
Another option would be to use a UIActionSheet and add the uitableview as a subview.
Here is a very similar question about how to add a UIPickerview with similar effect.
Add UIPickerView & a Button in Action sheet - How?

UIView Animation animates position but not width

I'm trying to transform a UISearchBar, like in Mobile Safari: touch in the search field and it grows while the location field shrinks.
My current animation to alter the width and position of the search field only animates the position: just before it slides to the right place, it simply snaps out to the right width. Here's my code:
[UIView beginAnimations:#"searchGrowUp" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationDelegate:self];
CGFloat findFieldWidth = findField.frame.size.width;
CGFloat urlFieldWidth = urlField.frame.size.width;
CGRect findFieldFrame = findField.frame;
CGRect urlFieldFrame = urlField.frame;
findFieldFrame.origin.x = findFieldFrame.origin.x - 150.0f;
findFieldFrame.size.width = findFieldWidth + 150.0f;
urlFieldFrame.size.width = urlFieldWidth - 150.0f;
urlField.frame = urlFieldFrame;
findField.frame = findFieldFrame;
[UIView commitAnimations];
I've modified this code slightly for the sake of presenting it here, but I hope this gives the gist.
Any guesses as to why this is happening would be appreciated!
Cheers,
Aaron.
I figured it out thanks to this post: Changing the size of the UISearchBar TextField?
Turns out the contents of a UISearchBar don't resize properly along with the outer layer. So you have to call -layoutSubviews: within the animation block after the frame is set on the searchbar. So the block ends like:
[findField setFrame:CGRectMake(findField.bounds.origin.y, findField.bounds.origin.y, findFieldWidth, findField.bounds.size.height)];
[findField layoutSubviews];
[UIView commitAnimations];
Props to Nick Farina!
I had the same problem when trying to animate the width of a UISearchBar.
On iOS 4 and later, I found that using UIViewAnimationOptionLayoutSubviews as an option for
+[UIView animateWithDuration:delay:options:animations:completion:]
fixed it.
The accepted answer works but according to Apple docs you "should not call this method". The clean solution is to use layoutIfNeeded method:
[UIView animateWithDuration:UINavigationControllerHideShowBarDuration
delay:0.f
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[searchBar setFrame: ...
[searchBar layoutIfNeeded];
...
Enjoy!
Sorry this is a bit old, but I was running into a similar problem.
I didn't want to use layoutSubviews because the documentation says you shouldn't call that method directly.
What I did to solve my problem was call sizeToFit on the subview within the animation block.