Skip to content

Instantly share code, notes, and snippets.

@jwilling
Created July 4, 2012 22:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwilling/3049829 to your computer and use it in GitHub Desktop.
Save jwilling/3049829 to your computer and use it in GitHub Desktop.
NSView add/removeSubview animated
@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