动画NSSplitview和窗口大小

时间:2012-08-13 09:34:56

标签: objective-c cocoa nsanimation nsanimationcontext

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

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

以下是我使用的代码:

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

1 个答案:

答案 0 :(得分:0)

如果您仔细查看NSSplitView的课程参考,您会发现它符合NSAnimatablePropertyContainer。这意味着NSSplitView将为您提供“自己的动画代理”。如果您在animator上致电NSSplitView,您将获得此代理,您可以在其上以动画方式更改属性。

要调整动画持续时间和计时功能,请按照您现有的方式使用NSAnimationContext

最后但并非最不重要:您是否认识到NSSplitView's minPossiblePositionOfDividerAtIndex:maxPossiblePositionOfDividerAtIndex: