我想实现的是,当用户按下按钮时,窗口会增长,多余的空间会被一个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];
}
如果你仔细观察NSSplitView
的类引用,你会发现它符合NSAnimatablePropertyContainer
。这意味着NSSplitView
将为您提供"自身的动画代理"。如果您在NSSplitView
上调用animator
,您将获得此代理,您应该能够以动画方式更改属性。
要调整动画持续时间和计时功能,请以与NSAnimationContext
相同的方式使用。
最后但并非最不重要的一点是:你认出NSSplitView's
minPossiblePositionOfDividerAtIndex:
和maxPossiblePositionOfDividerAtIndex:
吗?