Skip to content

Instantly share code, notes, and snippets.

@hhyyy9
Created September 29, 2012 01:30
Show Gist options
  • Save hhyyy9/3802858 to your computer and use it in GitHub Desktop.
Save hhyyy9/3802858 to your computer and use it in GitHub Desktop.
在调用 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/
//
#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 = [(NSNumber *)context floatValue];
self.layer.opacity = originalOpacity;
[self removeFromSuperview];
[(NSNumber *)context release];
}
}
- (void)removeFromSuperviewAnimated {
[UIView beginAnimations:@"fadeout" context:[[NSNumber numberWithFloat:self.layer.opacity] retain]];
[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