animation effects : leaf falling down to ground - iphone

Need to figure some transformation codes to implement falling down naturally just like leaves falling down from tree.
Here are some codes, fly from one place to another place with some rotation angle.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:5.0];
[UIView setAnimationDelegate:self];
//[UIView setAnimationRepeatCount:1e100f]; //coutless
[UIView setAnimationRepeatCount:1]; // 1 time
//[UIView setAnimationRepeatAutoreverses:YES];
leaf2.frame = CGRectMake(LEAF2_X, LEAF2_Y, LEAF2_W, LEAF2_H);
leaf2.transform = CGAffineTransformMakeRotation(ANGLE);
[UIView commitAnimations];

See if the next 2 questions help:
What is the best way to make a bouncing ball animation with infinite loop on iPhone?
Mimic UIAlertView Bounce?

Related

two UIView Animation on the same image

Well I would like to have two UIView animation on a same image in the same method like this :
-(void)likeThis{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
image.alpha=0;
[UIView commitAnimations];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
image.transform = CGAffineTransformScale(5,5);
[UIView commitAnimations];
}
But there is only one of these UIView Animation who work.I don't know why.I think there is another way to put two animation for the same image but I don't know how. sorry for my english I'm french :/
You can just put them in the same animation block:
-(void)likeThis
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.75];
[UIView setAnimationDelegate:self];
image.alpha=0;
image.transform = CGAffineTransformScale(image.transform,5,5);
[UIView commitAnimations];
}
Note that CGAffineTransformScale takes three arguments:
CGAffineTransformScale(image.transform, 5.0, 5.0)
See http://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGAffineTransform/Reference/reference.html#//apple_ref/doc/uid/TP30000946-CH1g-F16985
Or you could use CGAffineTransformMakeScale:
http://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGAffineTransform/Reference/reference.html#//apple_ref/c/func/CGAffineTransformMakeScale
Do you want them to happen simultaneously, or one after the other? If the former just include them both between a single set of beginAnimations..commitAnimations calls

UIView Animation isn't working

[UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelay:0.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:viewHelp cache:YES];
[self.view addSubview:viewHelp];
[UIView commitAnimations];
Where am I doing wrong? I am able to switch the view but I am not able to animate it.
[self.view addSubview:viewHelp]; the animation is just literally adding the subview, you need to work out what animation you want, and then apply it by changing values in the views frame property.?
follow this thread : click me

Trouble with UIView Animations - performing the same animation more than once doesn't work

I need to flip a card and then flip it back. I have written code to do that with no trouble. The trouble comes because users can flip this card back and forth as many times as they want, and it seems that the effect is cumulative. On the second try, the card spins like a top. I'm not completely surprised, as there only seems to be a way to add animation, not clear one from the view. Is there a way to 're-set' a UIView of any animations I had previously committed? Is there a way to capture it and re-use it without committing a new one? Or am I missing something obvious? This is my code:
if (self.flipped) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self
cache:YES];
[imageView removeFromSuperview];
[UIView commitAnimations];
self.flipped = NO;
} else {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self
cache:YES];
[self addSubview:imageView];
[UIView commitAnimations];
self.flipped = YES;
}
Thanks for reading.
To cancel all ongoing animations:
#import <QuartzCore/QuartzCore.h>
...
[view.layer removeAllAnimations];
To start a new animation that kills existing animations, if any, and starts the movement from wherever the view currently is — which I think is what you want:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
... and the rest of it ...
[UIView commitAnimations];
Hopefully one of those will solve your problem.
Whatever your "self" is, put it in a containing view called foo, and then change your setAnimation... line to this:
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:foo
cache:YES];

iPhone: How to commit two animations after another

This is an absolute beginner's question (sorry), but I was wondering how to commit one animations and once it has ended to start another one. Imagine having an image moved from x=0 to x=300. Then you want to do animate the same image again, but this time from x=300 to x=330 so that it disappears from the screen.
The following code will only do the animation from x=300 to x=330 and will not commit the animation x=0 to x=300. I'm sure I don't get the concept of commitAnnimation and that this is obvious, but how would I do two animations after one another?
I know I could move the image straightaway to 330, but I don't want this as I need the first animation (x 0 - 300) to be in sync with another animation.
Here is my (wrong) code:
[UIView beginAnimations:#"shadowMove" context:nil]; // Begin animation
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[imageView setFrame:CGRectOffset([imageView frame], 300, 0)];
[UIView commitAnimations]; // End animations
// Second Animation
[UIView beginAnimations:#"shadowMoveRestAway" context:nil]; // Begin animation
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[imageView setFrame:CGRectOffset([imageView frame], 330, 0)]; // Move imageView off screen
[UIView commitAnimations]; // End animations
Use the delegate method
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
to start something else when the first animation has finished. So put what you want to do next inside the animationDidStop:finished:context method, remember that the iphone is an event driven environment so linear code like you have above will simply kick off each animation at almost the same time.
EDIT:
I forgot to add that you need to set the animation delegate as well otherwise you won't get the event when the first one stops - see below;
Here's a full version of your code with the change - I'm using the abbreviated animationDidStop delegate as that's easier to understand and fine for this example.
[UIView beginAnimations:#"shadowMove" context:nil]; // Begin animation
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(myFirstAnimationDidStop)];
[imageView setFrame:CGRectOffset([imageView frame], 300, 0)];
[UIView commitAnimations]; // End animations
Then you just need a new method like this;
-(void) myFirstAnimationDidStop {
// Second Animation
[UIView beginAnimations:#"shadowMoveRestAway" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[imageView setFrame:CGRectOffset([imageView frame], 330, 0)];
[UIView commitAnimations];
}
And for completeness, in your interface (.h) file you should add;
-(void) myFirstAnimationDidStop;
#selector is easy, it's just a way of pointing to another method - hopefully this example clarifies that.

UIImageView Transform Scale

I have a UIButton that when pressed, will make a UIImageView scale slightly larger and then back to its normal size. It's working, but I'm running into a problem where the image eventually keeps getting slightly smaller the more you press the button.
How can I change the code so that the image doesn't get smaller as you press the button? Here's the code I have to scale the image slightly larger and then back to normal:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.2];
myImage.transform = CGAffineTransformScale(myImage.transform, 1.03, 1.03);
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.2];
myImage.transform = CGAffineTransformScale(myImage.transform, 0.97, 0.97);
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
Thanks for any help.
It's math. Scaling something by 1.03 * 0.97 results in scaling factor of 0.9991 and not 1.0. Either use 1.0/1.03 as your second scaling factor or just set myImage.transform to the identity transform (assuming you are not applying other transformations to that view).
Have you tried saving first transform?
And then instead of using CGAffineTransformScale to shrink your UIImageView you just use your saved transform?
CGAffineTransform firstTransform = myImage.transform;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.2];
myImage.transform = CGAffineTransformScale(myImage.transform, 1.03, 1.03);
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.2];
myImage.transform = firstTransform;
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
You are suffering from a limitation in floating point arithmetic and cumulative matrix operations. You will never be able to represent 3/100, only approximate it with values like, 0.03, 0.033, and 0.0333.
For your problem it would be better to set the second transform to the CGAffineTransformIdentity