Skip to content

Instantly share code, notes, and snippets.

@indragiek
Created September 11, 2011 03:11
Show Gist options
  • Save indragiek/1209118 to your computer and use it in GitHub Desktop.
Save indragiek/1209118 to your computer and use it in GitHub Desktop.
Animated NSView redraws + scrolling
@implementation NSView (SNRAdditions)
- (void)scrollPointAnimated:(NSPoint)point
{
NSScrollView *scrollView = [self enclosingScrollView];
NSClipView *clipView = [scrollView contentView];
NSPoint constrainedPoint = [clipView constrainScrollPoint:point];
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[clipView animator] setBoundsOrigin:constrainedPoint];
[NSAnimationContext endGrouping];
[scrollView reflectScrolledClipView:clipView];
}
- (NSImage*)NSImage
{
NSSize viewSize = [self bounds].size;
NSBitmapImageRep *bir = [self bitmapImageRepForCachingDisplayInRect:[self bounds]];
[bir setSize:viewSize];
[self cacheDisplayInRect:[self bounds] toBitmapImageRep:bir];
NSImage* image = [[NSImage alloc] initWithSize:viewSize];
[image addRepresentation:bir];
return image;
}
- (void)redrawAnimated
{
NSImageView *imageView = [[NSImageView alloc] initWithFrame:self.bounds];
imageView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
imageView.image = self.NSImage;
[self addSubview:imageView];
[self display];
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setCompletionHandler:^(void) {
[imageView removeFromSuperview];
}];
[[imageView animator] setAlphaValue:0.f];
[NSAnimationContext endGrouping];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment