Animating removeFromSuperview - ios

I'd like to animate the transition from a subview back to the super view.
I display the subview using:
[UIView beginAnimations:#"curlup" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[self.view addSubview:self.mysubview.view];
[UIView commitAnimations];
The above works fine. It's going back to the super view that I don't get any animation:
[UIView beginAnimations:#"curldown" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:.5];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[self.view removeFromSuperview];
[UIView commitAnimations];
Is there something different I should be doing to get the subview to animate when removed?

If you're targeting iOS 4.0 upwards you can use animation blocks instead:
[UIView animateWithDuration:0.2
animations:^{view.alpha = 0.0;}
completion:^(BOOL finished){ [view removeFromSuperview]; }];
(above code comes from Apple's UIView documentation)

I think you need to do forView:self.view.superview instead, to be consistent with what you are doing when you are adding, because in this case the self.view is the child, and so you would need to do it on the parent.

JosephH answer in Swift:
UIView.animateWithDuration(0.2, animations: {view.alpha = 0.0},
completion: {(value: Bool) in
view.removeFromSuperview()
})

Although approach with sending removeFromSuperview message from animation completion block works fine for most cases, sometimes there is no way to prevent a view from immediate removal from view hierarchy.
For example, MKMapView removes its subviews after it receives message removeAnnotations, and there is no "animated" alternative for this message in the API.
Nonetheless the following code allows you to do whatever you like with a visual clone of a view after it's been removed from superview or even deallocated:
UIView * snapshotView = [view snapshotViewAfterScreenUpdates:NO];
snapshotView.frame = view.frame;
[[view superview] insertSubview:snapshotView aboveSubview:view];
// Calling API function that implicitly triggers removeFromSuperview for view
[mapView removeAnnotation: annotation];
// Safely animate snapshotView and release it when animation is finished
[UIView animateWithDuration:1.0
snapshotView.alpha = 0.0;
}
completion:^(BOOL finished) {
[snapshotView removeFromSuperview];
}];

Example below:
func removeSpinningGear(cell: LocalSongsCollectionViewCell) {
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveLinear, animations: {
cell.spinningGearBtn.alpha = 0.0
}) { _ in
cell.spinningGearBtn.removeFromSuperview()
}
}

Related

IOS stop UIbutton to take its own place after animation completed

I have set the animation like seen in below image. In which UIbutton move from left to right and then top to bottom. Animation work correctly but after completion of animation UIButton comes to its original place before the segue perform. so, it's not look good. I want to set that after the completion of animation UIButton can't come to it's own place before segue .
Here is my try with Image.
//Move button Left to Right
- (IBAction)btnEasy:(id)sender {
Easy=YES;
NSLog(#"your x is: %f ", self.btnEasy.frame.origin.x);
NSLog(#"your y is: %f ", self.btnEasy.frame.origin.y);
x1=self.btnEasy.frame.origin.x;
y1=self.btnEasy.frame.origin.y;
CGRect screenRect = [[UIScreen mainScreen] bounds];
[UIView animateWithDuration:1.150 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.btnEasy.frame = CGRectMake(screenRect.size.width/1.80, self.btnEasy.frame.origin.y, self.btnEasy.frame.size.width, self.btnEasy.frame.size.height);
[self performSelector:#selector(btneasyanimation) withObject:self afterDelay:1.160 ];}
completion:^(BOOL finished) {
}];
//Move Button top to Bottom
if (Easy==YES) {
if (isiPad2 || isiPadAir || isiPadRatina) {
//[self.view layoutIfNeeded];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:1.0];
[_btnEasy setFrame:CGRectMake(self.view.frame.origin.x+290, self.view.frame.origin.y+900, self.btnEasy.frame.size.width, self.btnEasy.frame.size.height)];
[UIView commitAnimations];
}
else if (isiPhone4s) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:1.0];
[_btnEasy setFrame:CGRectMake(self.view.frame.origin.x+92, self.view.frame.origin.y+428, self.btnEasy.frame.size.width, self.btnEasy.frame.size.height)];
[UIView commitAnimations];
}
}
[self performSelector:#selector(segueeMethod) withObject:self afterDelay:1.160 ];
Image :-
If you are not placing directly the button given it the frame, thus using autolayout (autoresize will end with the same effect). You need to explicitly use autolayout, retain a reference to your constraints and update them (you can search here how to do that) and then set the UIView animation block [button layoutIfNeeded]
You can see a detailed answer about it in this answer
There is an easy and quick way that will work with your current implementation. Provided you know exactly where you want the view to be once the animation is done, you can do the following:
in UIView animateWithDuration: ... assign the final transform (position & orientation) of the view in the completion block, i.e.:
completion:^(BOOL finished) {
// Assign views transform and appearance here, for when the animation completes
}
Hope that helps.

Make popup of dataDetectorTypes (Textview) in front

i have a line separator of master and detail view(ipad) and stay always appeared when touching a textview , is that a way to make always the popup in front ?thanks..
for that you have to Create one UIView & Use like ,
//For Set Alert View
[[self.view superView] bringSubviewToFront: YOurCreatedView];
& for Dismiss Alert View
[[self.view superView] sendSubviewToBack: YOurCreatedView];
And For Animation Use below Method..
[UIView animateWithDuration:0.25 animations:^{
//
} completion:^(BOOL finished) {
//
}];
or Below Method..
CATransform3D transform3d = CATransform3DMakeScale(1.15, 1.15, 1.15);
cell.layer.transform = transform3d;
//4. Define the final state (After the animation) and commit the animation
[UIView beginAnimations:#"translation" context:NULL];
[UIView setAnimationDuration:0.8];
[[self.view superView] bringSubviewToFront: YOurCreatedView];
YOurCreatedView.alpha = 1;
YOurCreatedView.layer.transform = CATransform3DIdentity;
YOurCreatedView.layer.shadowOffset = CGSizeMake(0, 0);
[UIView commitAnimations];

Flip view itself using animateWithDuration

I am using UIView animateWithDuration method to flip the UILabelView itself but it doesn't show any animation until I provide some code in the animations block. Here's the code.
No effect
[UIView animateWithDurations:5. delay:0.
options:UIViewAnimationOPtionTransitionFlipFromLeft animations: ^{}
completion:nil];
Fades the View
[UIView animateWithDurations:5. delay:0.
options:UIViewAnimationOPtionTransitionFlipFromLeft
animations: ^{self.lblEvent.alpha = 0.5;} completion:nil];
My question is when the options parameter will effect?
I tried the below code too but it flips the view and removes from the controller. Can't I provide the same view?
[UIView transitionFromView: self.lblEvent toView:self.lblEvent
duration:2.0 options:UIViewAnimationOPtionTransitionFlipFromLeft
completion:nil];
Try this one:
[UIView transitionWithView:your_view_to_animate duration:1. options:(UIViewAnimationOptionTransitionFlipFromBottom) animations:^{
} completion:^(BOOL finished) {
}];
Good Luck!

Am I releasing this object too early after the animation?

I have a UIView and I am animating it.
The thing here is that I need to release theView once it is animated out.
originalRECT = [[UIScreen mainScreen] bounds];
if(theView)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
theView setFrame = originalRECT;
[UIView commitAnimations];
[theView autorelease];
theView = nil;
}
So I know the code is setting theView to nil, but the animation does finish ok (No SIGABRT or something like that)
Or is there a callback function I can use to know that the view disappeared? How can I use a function like that in this case?
Thanks!
Few things -
First of all if you want to be sure you can use:
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationCurveEaseInOut
animations:^ {
//your animation
theView setFrame = originalRECT;
}
completion:^(BOOL finished) {
//Animation finished you can release
[theView release];
}];
second - why do you use autoRelease and not release?
[theView release];
Third - Just for w meeter you don't know. USE ARC it will make your life a bit easier. You can read more here ARC tutorial

MKMapView fade in but won't fade out via animation

I've been trying to fade in an MKMapView when we have successfully found the address, and fade out the view when long & lat == 0. I have the code in the delegate:
- (void)didCompleteMapsRequestWithLatitude:(double)latitude andLongitude:(double)longitude
Simple actions work as expected (e.g., setHidden:YES or setHidden:NO) at the correct time.
My problem is that while the fade in seems to work well every time it's called, the fade out animation doesn't appear to happen. It's as if the only call is setHidden:YES.
My fade code is as follows:
//no location found
if (location.latitude == 0 && location.longitude == 0)
{
//fade out
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[self.map setAlpha:0.0];
[UIView commitAnimations];
[self.map setHidden:YES];
}
//we found the location on the map
else
{
[self.map setHidden:NO];
//fade in
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
[self.map setAlpha:1.0];
[UIView commitAnimations];
}
Note I get the same behaviour using animation blocks with iOS 4.0.
Any thoughts?
Thanks
I think you need to run [self.map setHidden:YES] when the animation completed, like this:
[UIView animateWithDuration:1.0
animations:^{
self.map.alpha=0.0;
}
completion:^(BOOL finished){
self.map.hidden=YES;
}];

Resources