16

I'm trying to resize an NSWindow (the main one) with a nice easing animation (EaseOut).

I can use the [NSWindow animator] but I havn't found a way to add the easing effect.

Do you have an idea or code sample which can help me to do that?

kaal101
  • 654
  • 7
  • 14

2 Answers2

37

Option 1

float Y = 100;
float X = 200;

NSRect frame = [window frame];

frame.origin.y -= Y;
frame.size.height += Y;
frame.size.width += X;

[window setFrame:frame display:YES animate:YES];

Option 2

float Y = 100;
float X = 200;

NSRect frame = [window frame];

frame.origin.y -= Y;
frame.size.height += Y;
frame.size.width += X;

NSDictionary *windowResize = @{
    NSViewAnimationTargetKey: window,
    NSViewAnimationEndFrameKey: [NSValue valueWithRect:frame]
};
NSDictionary *oldFadeOut = @{
    NSViewAnimationTargetKey: [NSNull null],
    NSViewAnimationEffectKey: NSViewAnimationFadeOutEffect
};
NSDictionary *newFadeIn = @{
    NSViewAnimationTargetKey: [NSNull null],
    NSViewAnimationEffectKey: NSViewAnimationFadeInEffect
};

NSArray *animations = @[windowResize, newFadeIn, oldFadeOut];
NSViewAnimation *animation = [[NSViewAnimation alloc] initWithViewAnimations: animations];

[animation setAnimationBlockingMode: NSAnimationBlocking];
[animation setAnimationCurve: NSAnimationEaseIn];
[animation setDuration: 2];     
[animation startAnimation]; 
Andrew
  • 7,630
  • 3
  • 42
  • 51
Anne
  • 26,765
  • 9
  • 65
  • 71
  • Hi Anne, thanks for the snippet but with your solution I don't have the nice easing effect, do you have an idea? – kaal101 Jun 03 '11 at 10:11
  • Using `animate:YES` makes it nice and smooth. Please clarify the _"nice easing effect"_ you require. – Anne Jun 03 '11 at 10:14
  • What I'm referring to is the *Animation Pacing* you can see in this [documentation](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Animation_Types_Timing/Articles/Timing.html) – kaal101 Jun 03 '11 at 10:21
  • Added working snippet using `NSAnimation` and `NSAnimationEaseIn`. – Anne Jun 03 '11 at 14:58
  • That's exactly what I was looking for! I was just missing the `[animation setAnimationCurve: NSAnimationEaseIn];` part. Thanks for your help ;) – kaal101 Jun 03 '11 at 17:37
  • For the past few hours I've been looking for a way to animate an NSWindow's size without simultaneously blocking any other UI animations, and option #2 (with NSAnimationNonblocking) did the job. Thank you! – Archagon Sep 15 '15 at 06:36
  • Please leave Anne's update in place. It works. Modernising it for so-called 'clarity' only introduced 3 compiler bugs and was therefore of no value. – ghr Jan 17 '17 at 12:03
  • What do we need the 'newFadeIn' and 'oldFadeOut' dictionaries for? In my testing the animation still works the exact same when you remove them. – Noah Nuebling Feb 12 '22 at 02:09
-6
[[NSWindow animator] setAlphaValue:0.0];

Don't forget to set the alpha value to 1.0 when you want to show the window again.

  • 2
    That'll hide the window. Plus you should hide it (for real) when done with the opacity animation via `setHidden:YES`. But that's not at all related to window resizing?? – Jay Feb 10 '16 at 16:48