How to store animations in obj-c? - ios

I've got 2 methods in my class and in each I've got an animation. My question is, can I store these animations in one array or something and refer to them by beginAnimation:name? If yes, how can I do this?
thanks for help
-(void)hideMenu{
[UIView beginAnimations:#"HIDE_MENU" context:nil];
[UIView setAnimationDuration:0.38];
[UIView setAnimationDelay:0.12];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationBeginsFromCurrentState:YES];
self.frame = CGRectMake(0, -1 * self.frame.size.height, self.frame.size.width, self.frame.size.height);
[UIView commitAnimations];
}
-(void)showMenu{
[UIView beginAnimations:#"SHOW_MENU" context:nil];
[UIView setAnimationDuration:0.38];
[UIView setAnimationDelay:0.12];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationBeginsFromCurrentState:YES];
self.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
[UIView commitAnimations];
}
-(void)handleShowMenu:(NSNotification*)note{
[self showMenu];
}
-(void)handleHideMenu:(NSNotification*)note{
[self hideMenu];
}

You can't store UIView animations as they are not objects. Even with blocks animation api introduced in 4.0, animations are still just actions performed by static class methods. Closest thing you can do is store the blocks and reuse them, or just do what you are doing and keep compatibility with iOS versions older than 4.0.
If you really want to store your animations as objects, you should skip UIView animation and go a bit lower to Core Animation.

Related

Animation problems in iOS

I used some animations in my application.Its working fine.But while calling another animation previous animation comes old position.How to resolve these kind of problems.
Here my code:
CGRect frame = self.PickerView.frame;
frame.origin.x=0;
frame.origin.y=730;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
self.PickerView.frame=frame;
[UIView commitAnimations];

Slow down UIView animation like as the dropdown Menu in iOS

I have a problem with animation show UIView slow down like as dropdown menu. I tried below code but it so fast.
Can you help me to correct it? Thanks.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
blockView.frame = CGRectMake(newFrame.origin.x, newFrame.origin.y , newFrame.size.width, newFrame.size.height + *h);
[blockView addSubview:dropDownView];
[UIView commitAnimations];
I want to make UIView slow down like as the Expedia app. Please give me some advice.
Thanks in advance.
Just increase the AnimationDuration based on how much slow u want to make your drop down.
Exmaple:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.5];
self.blackView.frame = CGRectMake(newFrame.origin.x, newFrame.origin.y , newFrame.size.width, newFrame.size.height + h);
[self.blackView addSubview:self.dropDownView];
[UIView commitAnimations]
if u feel your dropDownView attached before the animation gets complete means u can use following code to attach your dropDownView at end of the animation completion.
[UIView animateWithDuration:5.5
animations:^{
self.blackView.frame = CGRectMake(newFrame.origin.x, newFrame.origin.y , newFrame.size.width, newFrame.size.height + h);
}
completion:^(BOOL finished){
[self.blackView addSubview:self.dropDownView];
}];
Use the options in UIView animation
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
// ... do stuff here
} completion:NULL];

Resize an image but never came back to original size

I'm trying to resize an image (tiny and return to original size). This code is working for resize the image but never go back to the original size. I thin I miss something in the endItem1Animation code. Any suggestions? Thanks
if (CGRectIntersectsRect(image.frame, item1.frame)) {
item1.hidden = YES;
// Item 1 Animation (Go Tiny)
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationsEnabled:YES];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:0];
[UIView setAnimationRepeatCount:0];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatAutoreverses:NO];
CGRect newRect = image.frame;
newRect.size = CGSizeMake(25,25);
image.frame = newRect;
[UIView commitAnimations];
[self endItem1Animation];
This is the endItem1Animation code:
// Item 1 End Animation (Return to Original Size)
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationsEnabled:YES];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:5.0];
[UIView setAnimationRepeatCount:0];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatAutoreverses:NO];
CGRect newRect = image.frame;
newRect.size = CGSizeMake(50,50);
image.frame = newRect;
[UIView commitAnimations];
The problem is that when you call beginAnimations / endAnimation, this does not block the main thread from running, so calling [self endItem1Animation] sets the second animation which will prevent the first animation from running.
You'll want to use the UIView animation block method instead, which lets you supply a completion handler when the animation completes.
[UIView animateWithDuration:0.5 animations:^{
CGRect newRect = image.frame;
newRect.size = CGSizeMake(25,25);
image.frame = newRect;
} completion:^(BOOL finished) {
[self endItem1Animation];
}];
Another, more straightforward issue, is that the duration for the first animation is zero.
I fix this issue running a selector at the end of the code. Now my code is working. Here is my final code, I Hope to help someone. Thank you for your suggestions.
if (CGRectIntersectsRect(image.frame, item1.frame)) {
item1.hidden = YES;
// Item 1 Animation (Go Tiny)
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationsEnabled:YES];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:0];
[UIView setAnimationRepeatCount:0];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatAutoreverses:NO];
CGRect newRect = image.frame;
newRect.size = CGSizeMake(25,25);
image.frame = newRect;
[UIView commitAnimations];
[self performSelector:#selector(endItem1Animation) withObject:nil afterDelay:8.0];

Press a button and it appears somewhere else on the screen

I have this code so it changes location once but then stays there when you still touch
- (IBAction)fade {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[button setAlpha: 0];
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[button setAlpha: 1];
[UIView commitAnimations];
button.center = CGPointMake(152 + 16,326 + 16);
button.center = CGPointMake(200 + (kPlatformWidth/2),300 + (kPlatformHeight/2));
}
How to make it go to a random location from here?
You should not use beginAnimations:context:
To quote the docs:
Use of this method is discouraged in iOS 4.0 and later. You should use
the block-based animation methods to specify your animations instead.
You should use the new methods of the form animateWithDuration:animations:
As for how to make the button move to a random location, use arc4random_uniform to calculate new x and y positions and use those values as the destination of your view's center.

removing UIView Animation acceleration

How does one remove the acceleration and deceleration animation that UIView Animations do?
If you don't understand what I mean, when an animation starts up it accelerates and then when it get to the end, it decelerated. I want the animation to be a constant speed.
Here is an example of what my code looks like (I MUST use the old animation ways).
-(void)animate:(NSString*)animationID finished:(BOOL)finished context:(void*)context {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDidStopSelector:#selector(animateStop:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:3];
Motion.center = (*PathPoints)[Location];
[UIView commitAnimations];
}
[UIView setAnimationCurve: UIViewAnimationCurveLinear];

Resources