Search code examples
cocoacore-animationnswindow

Resize NSWindows with easing animation


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?


Solution

  • 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];