The following method that i created for animation:
-(void)shakingAnimationForGate:(UIImageView *)image leftShaking:(CGAffineTransform)leftTransform rightShaking:(CGAffineTransform)rightTransform animationName:(NSString *)string{
[UIView animateWithDuration:5.0
delay:0.5
options: UIViewAnimationOptionCurveEaseOut
animations:^{
image.transform = leftTransform; // starting point
[UIView beginAnimations:string context:(__bridge void *)(image)];
[UIView setAnimationRepeatAutoreverses:YES]; // important
[UIView setAnimationRepeatCount:20];
[UIView setAnimationDelay:0.1];
[UIView setAnimationDuration:0.06];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(shakeEnded:finished:context:)];
image.transform = rightTransform; // end here & auto-reverse
[UIView commitAnimations];
}
completion:^(BOOL finished){
NSLog(#"end of shaking");
}];
}
which makes UIView do the "shaking".
Then i'm using this method:
CGAffineTransform leftShake = CGAffineTransformMakeTranslation(-2, 6);
CGAffineTransform rightShake = CGAffineTransformMakeTranslation(0, 3);
[self shakingAnimationForGate:imageLeft leftShaking:leftShake rightShaking:rightShake animationName:#"left view shake"];
where imageLeft is UIImageView.
So my problem is that this method don't do animation of shaking on iOS 6.1. It's working fine on iOS 7 though. Any ideas what's the problem?
When compiling, Xcode optimises your code a little. For example if you execute the following code, only the last line is executed:
[self.view setBackgroundColor:[UIColor greenColor]];
[self.view setBackgroundColor:[UIColor yellowColor]];
Most likely that's the case with the animation as well.
Move the revert part to the completion part.
Related
I want to do some action once animation ended.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.80f];
self.view.transform =
CGAffineTransformMakeTranslation(
self.view.frame.origin.x,
480.0f + (self.view.frame.size.height/2) // move the whole view offscreen
);
[self.view setAlpha:0];
[UIView commitAnimations];
I have done animation like above, How to find out animation ended, so that I can do my action after that.
Use this:
[UIView animateWithDuration:0.80f animations:^{
self.view.transform =
CGAffineTransformMakeTranslation(
self.view.frame.origin.x,
480.0f + (self.view.frame.size.height/2) // move the whole view offscreen
);
[self.view setAlpha:0];
}
completion:^(BOOL finished){
// your code
}];
Add this to your animation:
[UIView setAnimationDidStopSelector:#selector(myAnimationEnded)];
[UIView setAnimationDelegate:self];
and this method will tell you when it stops;
- (void)myAnimationEnded{
NSLog(#"animation ended");
}
Use the UIView's -animateWithDuration:animations:completion: class method.
[UIView animateWithDuration:0.8 animations:^{
CGAffineTransformMakeTranslation(
self.view.frame.origin.x,
480.0f + (self.view.frame.size.height/2) // move the whole view offscreen
);
[self.view setAlpha:0];
} completion:^(BOOL finished) {
//this block is called when the animation is over
}];
Write your code after [UIView commitAnimations];. The animation remains between the beginAnimations and the commitAnimations.
Not that setAnimationDelegate
Use of this method is discouraged in iOS 4.0 and later. If you are using the block-based animation methods, you can include your delegate’s start and end code directly inside your block.
I have the same animation code applied to various views in my object, so i thought I would standardise it with it's own class. Here is the code i have:
.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface AHAnimations : NSObject
+(void)popInView:(UIView *)view;
#end
.m
#import "AHAnimations.h"
#implementation AHAnimations
+(void)popInView:(UIView *)view {
view.alpha = 0;
view.transform = CGAffineTransformMakeScale(0.9, 0.9);
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
view.alpha = 1;
view.transform = CGAffineTransformMakeScale(1, 1);
}completion:nil];
}
#end
But instead of animating, the view just appears.
Any ideas why this isn't working?
EDIT: The original code is exactly the same. I can paste it, but other than being a different method name, i'd just be copying and pasting it from above. And now, it is simply [AHAnimations popInView:view];
UIView *myview=[[UIView alloc] init];
[myview setFrame:CGRectMake( 50.0f, 50.0f, 280.0f, 400.0f)];
[UIView commitAnimations];
[UIView beginAnimations:#"animateTableView" context:nil];
[UIView setAnimationDuration:0.7];
[myview setFrame:CGRectMake( 5.0f, 70.0f, 310.0f, 350.0f)];
// CALL This line to stop animation
[myview.layer removeAllAnimations];
[UIView commitAnimations];
+[UIView animateWithDuration ... not animating all properties, tho alpha should work...
These code won't work, you can use CAAnimationGroup or CABasicAnimation's property: fromValue, byValue, toValue.
Also, you could implement like this which is easier to understand,
//go
- (void)stepOne{
[UIView beginAnimations:#"" context:nil];
[UIView setAnimationDuration:0.2];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(stepTwo)];
view.alpha = 0;
view.transform = CGAffineTransformMakeScale(0.9, 0.9);
[UIView commitAnimations];
}
//back
- (void)stepTwo{
[UIView beginAnimations:#"" context:nil];
[UIView setAnimationDuration:0.1];
view.alpha = 1;
view.transform = CGAffineTransformMakeScale(1, 1);
[UIView commitAnimations];
}
There are also a lot of original transition animation, you can check them out.
I am trying to remove a subview form a view with animation like below. However, when I click a button to run the code, the view is removed immediately. Does anyone know what happen here.
Thanks,
CGRect rect=[self.view viewWithTag:10].frame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationDidStopSelector:#selector(removeLayer)];
[[self.view viewWithTag:10] setFrame:CGRectMake(rect.origin.x, btn.frame.origin.y, rect.size.width, 0)];
[UIView animateWithDuration:1.0 animations:^{[[self.view viewWithTag:10] setFrame:CGRectMake(rect.origin.x, btn.frame.origin.y, rect.size.width, 0)];} completion:^(BOOL finished){[[self.view viewWithTag:10] removeFromSuperview];}];
[UIView commitAnimations];
-(void)removeLayer{
[[self.view viewWithTag:10] removeFromSuperview];
}
It looks like you are trying to use two different animation methods simultaneously. All you need is:
CGRect rect=[self.view viewWithTag:10].frame;
[UIView animateWithDuration:1.0 animations:^{[[self.view viewWithTag:10] setFrame:CGRectMake(rect.origin.x, btn.frame.origin.y, rect.size.width, 0)];} completion:^(BOOL finished){[[self.view viewWithTag:10] removeFromSuperview];}];
You are combining both types of animation API - I think the block-based one is returning immediately so your didStopSelector is being called straight away.
Try just using this part of it:
[UIView animateWithDuration:1.0 animations:^{[[self.view viewWithTag:10] setFrame:CGRectMake(rect.origin.x, btn.frame.origin.y, rect.size.width, 0)];} completion:^(BOOL finished){[[self.view viewWithTag:10] removeFromSuperview];}];
I think that you forgot to set animation delegate:
[UIView setAnimationDelegate:self];
In your code UIView doesn't know to which object it should send "removeLayer" selector
I'm trying to instigate a page curl transition with a UIImageView in a Window. This code is in my main init method :
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.5];
[UIView setAnimationDelay:delay];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDidStopSelector:#selector(animCompleteHandler:finished:context:)];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:splashImage cache:YES];
splashImage.frame = CGRectMake(-320, 0, 10, 10);
//[splashImage removeFromSuperview];
[UIView commitAnimations];
The image animates position and size but no curl. If I uncomment the removeFromSuperView it just vanishes instantly. Any ideas?
UPDATE:
Have changed the code so it uses Lars fantasticlly neat way of triggering an animation and including the animation and callback ...
[UIView animateWithDuration:1.5
delay:delay
options: UIViewAnimationTransitionCurlUp
animations:^{splashImage.alpha = 0;}
completion:^(BOOL finished){[splashImage removeFromSuperview];}
];
Unfortunately the page curl just doesn't happen. It does fade though.
I'm not sure if this is something to do with the syntax or the fact that the SplashImage is a UIImageView class in the UIWindow object of my main view. Maybe it needs to be in a UIView to create the transition.
Try something like:
[UIView transitionWithView:splashImage
duration:1.5
options: UIViewAnimationOptionTransitionCurlUp
animations^{
splashImage.frame = CGRectMake(-320, 0, 10, 10);
}
completion:^(BOOL finished){
[splashImage removeFromSuperview];
//animCompleteHandlerCode..
}
];
Not tested and maybe some syntax errors but give it a try!
Or maybe this is better:
[UIView animateWithDuration:1.5
delay:delay
options: UIViewAnimationOptionTransitionCurlUp
animations^{
splashImage.frame = CGRectMake(-320, 0, 10, 10);
}
completion:^(BOOL finished){
[splashImage removeFromSuperview];
//animCompleteHandlerCode..
}
];
#Larsaronen: Thanks for the examples, it was exactly what I needed! I just wanted a simple page curl while images are first displayed, so I used an alpha of 1.0 instead of 0, and set the completion callback to nil:
// set a page curl animation for the specified UIImageView control
- (void) setAnimationPageCurl:(UIImageView *)imageView {
[UIView transitionWithView:imageView
duration:1.5
options:UIViewAnimationOptionTransitionCurlDown
animations:^ { imageView.alpha = 1.0; }
completion:nil];
}
I have 2 layer is top and bottom on my program
how can i remove top layer with animation or bring top layer to back is it possible?
The easiest is playing with frame and alpha before removing it.
You can get some cool effects
-(void)removeWithEffect:(UIView *)myView
{
[UIView beginAnimations:#"removeWithEffect" context:nil];
[UIView setAnimationDuration:0.5f];
//Change frame parameters, you have to adjust
myView.frame = CGRectMake(0,0,320,480);
myView.alpha = 0.0f;
[UIView commitAnimations];
[myView performSelector:#selector(removeFromSuperview) withObject:nil afterDelay:0.5f];
}
iOS Update
You can now use blocks to perform your animation
[UIView animateWithDuration:0.5f
animations:^{view.alpha = 0.0;}
completion:^(BOOL finished){ [view removeFromSuperview]; }];
If you're targetting iOS 4.0 upwards you can use animation blocks:
[UIView animateWithDuration:0.2
animations:^{view.alpha = 0.0;}
completion:^(BOOL finished){ [view removeFromSuperview]; }];
(above code comes from Apple's UIView documentation)