Skip to content

Instantly share code, notes, and snippets.

@jamztang
Created October 3, 2011 18:35
Show Gist options
  • Save jamztang/1259860 to your computer and use it in GitHub Desktop.
Save jamztang/1259860 to your computer and use it in GitHub Desktop.
Adding fade out effect on -[UIView removeFromSuperview]
//
// UIView+JTRemoveAnimated.h
//
// Created by james on 9/1/11.
// http://ioscodesnippet.tumblr.com/
//
@interface UIView (JTRemoveAnimated)
- (void)removeFromSuperviewAnimated;
@end
//
// UIView+JTRemoveAnimated.m
//
// Created by james on 9/1/11.
// http://ioscodesnippet.tumblr.com/
//
// Updated at 10/12/12 for ARC projects
#import "UIView+JTRemoveAnimated.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIView (JTRemoveAnimated)
// remove static analyser warnings
#ifndef __clang_analyzer__
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
if ([animationID isEqualToString:@"fadeout"]) {
// Restore the opacity
CGFloat originalOpacity = [(__bridge_transfer NSNumber *)context floatValue];
self.layer.opacity = originalOpacity;
[self removeFromSuperview];
}
}
- (void)removeFromSuperviewAnimated {
[UIView beginAnimations:@"fadeout" context:(__bridge_retained void *)[NSNumber numberWithFloat:self.layer.opacity]];
[UIView setAnimationDuration:0.3];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];
self.layer.opacity = 0;
[UIView commitAnimations];
}
#endif
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment