开发者

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

开发者 https://www.devze.com 2023-02-03 02:00 出处:网络
When I call my [window setFrame: frame animated:YES] to en开发者_如何学Golarge the window it works great but it animates up and it goes behind the status bar. How can I make the window animate down in

When I call my [window setFrame: frame animated:YES] to en开发者_如何学Golarge 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?


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


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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消