如何使NSWindow的setFrame:..animated:YES]函数向下动画而非向上?

4
当我调用[window setFrame:frame animated:YES]来放大窗口时,它可以很好地工作,但它会向上动画并且会在状态栏后面。我该如何使窗口向下动画呢?
2个回答

13

只需要将y坐标减去增加的高度即可。

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

这就是我最终做的事情,理想情况下我想使用内置的动画函数来完成,但我没有找到方法。 - Ryan Detzel
2
这使用内置动画。 - ughoavgfhw

3

我找不到一种简单的方法来实现这个功能,所以我编写了一个动画函数,可以同时调整窗口大小和位置,从而使其看起来像是正在向下动画。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接