Ryan Detzel
Ryan Detzel

Reputation: 5599

How can I make the NSWindow setFrame: .. animated:YES] function animated down instead of up?

When I call my [window setFrame: frame animated:YES] to enlarge the window it works great but it animates up and it goes behind the status bar. How can I make the window animate down instead?

Upvotes: 4

Views: 5005

Answers (2)

ughoavgfhw
ughoavgfhw

Reputation: 39905

Just decrease the y coordinate the same distance you increase the height.

CGFloat amountToIncreaseWidth = 100, amountToIncreaseHeight = 100;
NSRect oldFrame = [window frame];
oldFrame.size.width += amountToIncreaseWidth;
oldFrame.size.height += amountToIncreaseHeight;
oldFrame.origin.y -= amountToIncreaseHeight;
[window setFrame:oldFrame animated:YES];

Upvotes: 13

Ryan Detzel
Ryan Detzel

Reputation: 5599

I couldn't find an easy way to do this so I wrote an animation function that would resize the window and move it at the same time thus giving the appearance that it's animating down.

- (IBAction)startAnimations:(id)sender{

    NSViewAnimation *theAnim;
    NSRect firstViewFrame;
    NSRect newViewFrame;
    NSMutableDictionary* firstViewDict;

    {
        firstViewDict = [NSMutableDictionary dictionaryWithCapacity:3];
        firstViewFrame = [window frame];
        [firstViewDict setObject:window forKey:NSViewAnimationTargetKey];

        [firstViewDict setObject:[NSValue valueWithRect:firstViewFrame]
                          forKey:NSViewAnimationStartFrameKey];

        newViewFrame = firstViewFrame;

        newViewFrame.size.height += 100;
        newViewFrame.origin.y -= 100;

        [firstViewDict setObject:[NSValue valueWithRect:newViewFrame]
                          forKey:NSViewAnimationEndFrameKey];
    }

    theAnim = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray
                                                               arrayWithObjects:firstViewDict, nil]];

    [theAnim setDuration:1.0]; 
    [theAnim startAnimation];

    [theAnim release];
}

Upvotes: 3

Related Questions