Created
July 4, 2012 22:28
-
-
Save jwilling/3049829 to your computer and use it in GitHub Desktop.
NSView add/removeSubview animated
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation NSView (Animations) | |
- (void)addSubview:(NSView *)aView animated:(BOOL)animated { | |
[aView setAlphaValue:0.f]; | |
[aView setFrameOrigin:NSZeroPoint]; | |
CGFloat duration = animated ? (([[[self window] currentEvent] modifierFlags] & NSShiftKeyMask) ? 1.f : 0.25f ) : 0.f; | |
[NSAnimationContext beginGrouping]; | |
[[NSAnimationContext currentContext] setDuration:duration]; | |
[self addSubview:aView]; | |
[[aView animator] setAlphaValue:1.f]; | |
[NSAnimationContext endGrouping]; | |
} | |
- (void)removeFromSuperviewAnimated:(BOOL)animated { | |
CGFloat duration = animated ? (([[[self window] currentEvent] modifierFlags] & NSShiftKeyMask) ? 1.f : 0.25f ) : 0.f; | |
[NSAnimationContext beginGrouping]; | |
[[NSAnimationContext currentContext] setDuration:duration]; | |
[[self animator] setAlphaValue:0.f]; | |
[NSAnimationContext endGrouping]; | |
// if we were Lion-only, using built-in completion handler would be a better idea | |
[self performSelector:@selector(removeFromSuperview) | |
withObject:nil | |
afterDelay:duration]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment