0

我想要实现的是,当用户按下按钮时,窗口会增长,额外的空间被一个展开 NSSplitview并增长以填充空间的面板占用。

我可以轻松地为窗口调整大小和拆分视图独立增长设置动画,但是当我尝试将两个动画放在一起时,一个不可避免地会在另一个之前或逐步发生。首先窗口部分调整大小,然后拆分视图调整大小,然后窗口完成调整大小,最后拆分视图也完成。关于为什么会发生这种情况的任何想法?

这是我使用的代码:

- (IBAction)helpButtonPressed:(id)sender
{
   if ([sender isKindOfClass:[NSMenuItem class]]) [sender setState:![sender state]];
   NSWindow *window = [[[self windowControllers] objectAtIndex:0] window];
   NSRect oldFrame = [window frame];
   CGFloat windowWidthAdd;

   if ([sender state]) windowWidthAdd = HELP_WIDTH; else windowWidthAdd = -HELP_WIDTH;    
   NSRect newFrame = NSMakeRect(oldFrame.origin.x, oldFrame.origin.y,oldFrame.size.width+windowWidthAdd, oldFrame.size.height);

   [[NSAnimationContext currentContext] setDuration:0.3f];
   [NSAnimationContext beginGrouping];
   [[window animator] setFrame:newFrame display:YES];
   if ([sender state]) [self uncollapseRightView]; else [self collapseRightView];
   [NSAnimationContext endGrouping];

}

-(void)collapseRightView
{
    NSView *right = [[self.splitView subviews] objectAtIndex:1];
    NSView *left  = [[self.splitView subviews] objectAtIndex:0];
    NSRect leftFrame = [left frame];
    NSRect overallFrame = [self.splitView frame];
    [right setHidden:YES];
    [[left animator] setFrameSize:NSMakeSize(overallFrame.size.width,leftFrame.size.height)];
}

-(void)uncollapseRightView
{
    NSView *left  = [[self.splitView subviews] objectAtIndex:0];
    NSView *right = [[self.splitView subviews] objectAtIndex:1];
    [right setHidden:NO];
    CGFloat dividerThickness = [self.splitView dividerThickness];
    // get the different frames
    NSRect leftFrame = [left frame];
    // Adjust left frame size
    leftFrame.size.width = (leftFrame.size.width-HELP_WIDTH-dividerThickness);
    NSRect rightFrame = [right frame];
    rightFrame.origin.x = leftFrame.size.width + dividerThickness;
    rightFrame.size.width = HELP_WIDTH;
    [[left animator] setFrameSize:leftFrame.size];
    [[right animator] setFrame:rightFrame];
}
4

1 回答 1

0

如果您仔细查看 的类引用NSSplitView,您会发现它符合NSAnimatablePropertyContainer. 这意味着NSSplitView将为您提供“自身的动画代理”。如果你打电话animator给你NSSplitView,你会得到这个代理,你应该能够以动画方式更改属性。

要调整动画持续时间和计时功能,请使用NSAnimationContext您已经做过的相同方式。

最后但并非最不重要的一点:你认识NSSplitView's minPossiblePositionOfDividerAtIndex:maxPossiblePositionOfDividerAtIndex:吗?

于 2013-02-17T10:03:23.163 回答