Is it possible to removeFromSuperview with Animation? - iphone

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)

Related

iOS 6.1 animation not working

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.

How to get animation end notification

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.

Running an animation one after the other fails

I'm trying to animate an image to rotate left and right when selected, basically to let the user which the object they're touching.
I found some code for animating:
- (void)rotateImage:(UIImageView *)image duration:(NSTimeInterval)duration
curve:(int)curve degrees:(CGFloat)degrees delay:(CGFloat)delay
{
// Setup the animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelay:delay];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:curve];
[UIView setAnimationBeginsFromCurrentState:YES];
// The transform matrix
CGAffineTransform transform =
CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(degrees));
image.transform = transform;
// Commit the changes
[UIView commitAnimations];
}
However, when I try to run two animations, only the last one ever works. Even with a proper delay, only the second animation will show:
[self rotateImage:self duration:.5
curve:UIViewAnimationCurveEaseIn degrees:60 delay:0];
[self rotateImage:self duration:.5
curve:UIViewAnimationCurveEaseIn degrees:-60 delay:5];
What can I do to create the animation so it rotates left, then rotates right?
[UIView animateWithDuration:0.2 animations:^{
// animation left
[UIView setAnimationDelay:10];
} completion:^(BOOL finished){
[UIView animateWithDuration:0.2 animations:^{
// animation right
} completion:^(BOOL finished){
// done
}];
}];
found it here
https://developer.apple.com/library/content/featuredarticles/Short_Practical_Guide_Blocks/
[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(60));
} completion:^(BOOL finished){
[UIView animateWithDuration:0.5f delay:5.0f options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-60));
}
completion:^(BOOL finished){}];
}];
Animation blocks are definitely the way to go. This should replicate what you're trying to do, including the easing. This assumes your calling from a view controller, but if your putting this code into a UIView or UIImageView, then just replace self.view with self.
[UIView animateWithDuration:0.5f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(60));
} completion:^(BOOL finished){}];
[UIView animateWithDuration:0.5f delay:5.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
self.view.transform = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(-60));
} completion:^(BOOL finished){}];

How to unhide view with animations

Say I have a hidden view in Xcode for iOS. Now, when I set the view to not hidden (view.hidden=NO), how can I make it so that it now appears, but with animations?
What you probably want is not to set view.hidden, but to set view.alpha to 0 (corresponds to hidden = YES) or 1 (hidden = NO).
You can then use implicit animations to show the view, e.g
[UIView animateWithDuration:0.3 animations:^() {
view.alpha = 1.0;
}];
If you want other animations than only fading then use this method
[UIView transitionWithView:_yourView duration:1.0 options:UIViewAnimationOptionTransitionCurlDown animations:^(void){
[_yourView setHidden:NO];
} completion:nil];
For a fade, you can adjust the alpha property of the view.
myView.alpha = 0;
[UIView animateWithDuration:0.5 animations:^{
myView.alpha = 1;
}];
That will apply a fade in over 0.5 seconds to the view myView. Many UIView properties are animatable; you aren't just limited to alpha fades. You can change background colours, or even rotate and scale a view, with animation. If you need further control and advanced animation, you can then move into Core Animation - a much more complex animation framework.
-(void)showView{
[UIView beginAnimations: #"Fade Out" context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:.5];
//show your view with Fade animation lets say myView
[myView setHidden:FALSE];
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(hideView) userInfo:nil repeats:YES];
[UIView commitAnimations];
}
-(void)hideView{
[UIView beginAnimations: #"Fade In" context:nil];
[UIView setAnimationDelay:0];
[UIView setAnimationDuration:.5];
//hide your view with Fad animation
[myView setHidden:TRUE];
[UIView commitAnimations];
}
OR you can try this way
self.yourView.alpha = 0.0;
[UIView beginAnimations:#"Fade-in" context:NULL];
[UIView setAnimationDuration:1.0];
self.yourView.alpha = 1.0;
[UIView commitAnimations];

UIView removeFromSuperview

The removeFromSuperview method actually pulls off the image very quickly, is there a way to remove it in a animated fashion. thanks
If you're targeting iOS4 (code snippet from UIView docs):
[UIView animateWithDuration:0.2
animations:^{ table.alpha = 0.0; }
completion:^(BOOL finished){ [table removeFromSuperview]; }];
code example here
http://www.iphonedevsdk.com/forum/iphone-sdk-development/37467-removefromsuperview-animation-transition.html
1) insert your new subview (index of 0 so it's hidden) onto the view stack.
[window insertSubview:myNewView atIndex:0];
2) Then animation the view out (straight from the docs) is:
[UIView animateWithDuration:0.2
animations:^{ view.alpha = 0.0; }
completion:^(BOOL finished){ [view removeFromSuperview]; }]