0

我想在以NSSplitView编程方式调整大小时制作动画。这是调整视图大小的代码SplitView

在 awakeFromNib 中:我正在观察NSSplitViewWillResizeSubviewsNotification

 [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(splitViewWillResizeSubviewsHandler:)
            name:NSSplitViewWillResizeSubviewsNotification
            object:splitView];

拆分视图调整大小

NSView * leftSubView = [[splitView subviews] objectAtIndex:0];       
if ([leftSubView isHidden]) 
{
    [leftSubView setHidden:NO];
    [splitView setPosition:0 ofDividerAtIndex:0];
}
else 
{
    [leftSubView setHidden:YES];
}
[splitView adjustSubviews];
    [[NSNotificationCenter defaultCenter] postNotificationName:NSSplitViewWillResizeSubviewsNotification object:self userInfo:nil];

我可以调整 splitView 的大小。我应该在哪里添加动画代码。(当 rightView 移动以替换 leftSubView 时,我希望调整大小发生一些延迟)?

我在调整大小之前和之后都有两个视图的矩形。我应该在哪里编写动画代码。

4

1 回答 1

1

我能够解决动画。以下代码对我有用

-(IBAction)resizeViews:(id)sender
    {
        NSSplitView *splitView = [self splitView];
        NSView * leftSubView = [[splitView subviews] objectAtIndex:0];
        NSView * rightSubView = [[splitView subviews] objectAtIndex:1];
        NSLog(@"splitView Frame %@",NSStringFromRect(splitView.frame));
        NSLog(@"left Frame %@",NSStringFromRect(leftSubView.frame));
        NSLog(@"right Frame %@",NSStringFromRect(rightSubView.frame));

        self.lastLeftViewWidth = leftSubView.frame.size.width;

        NSMutableDictionary *collapseMainAnimationDict = [NSMutableDictionary dictionaryWithCapacity:2];
        [collapseMainAnimationDict setObject:rightSubView forKey:NSViewAnimationTargetKey];
        NSRect newRightSubViewFrame = rightSubView.frame;
        newRightSubViewFrame.size.width =  splitView.frame.size.width;
        [collapseMainAnimationDict setObject:[NSValue valueWithRect:newRightSubViewFrame] forKey:NSViewAnimationEndFrameKey];

        NSMutableDictionary *collapseInspectorAnimationDict = [NSMutableDictionary dictionaryWithCapacity:2];
        [collapseInspectorAnimationDict setObject:leftSubView forKey:NSViewAnimationTargetKey];
        NSRect newLeftSubViewFrame = leftSubView.frame;
        newLeftSubViewFrame.size.width = 0.0f;
        newLeftSubViewFrame.origin.x = splitView.frame.size.width;
        [collapseInspectorAnimationDict setObject:[NSValue valueWithRect:newLeftSubViewFrame] forKey:NSViewAnimationEndFrameKey];

        NSViewAnimation *collapseAnimation = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:collapseMainAnimationDict, collapseInspectorAnimationDict, nil]];
        [collapseAnimation setDuration:0.60f];
        [collapseAnimation startAnimation];
        [splitView adjustSubviews];
        [splitView setNeedsDisplay:YES];
    }

-(IBAction)normalizeViews:(id)sender
{
    NSView * left = [[self.splitView subviews] objectAtIndex:0];
    NSView * right = [[self.splitView subviews] objectAtIndex:1];
    NSLog(@"splitView Frame %@",NSStringFromRect( self.splitView.frame));
    NSLog(@"left Frame %@",NSStringFromRect( left.frame));
    NSLog(@"right Frame %@",NSStringFromRect( right.frame));
//    [right setFrame: NSMakeRect(0, right.frame.origin.y, right.frame.size.width-118, right.frame.size.height)];
//    [left setFrame:NSMakeRect(0, 0, 118, left.frame.size.height)];

    left.hidden = NO;

    NSMutableDictionary *expandMainAnimationDict = [NSMutableDictionary dictionaryWithCapacity:2];
    [expandMainAnimationDict setObject:right forKey:NSViewAnimationTargetKey];
    NSRect newMainFrame = right.frame;
    newMainFrame.size.width =  self.splitView.frame.size.width-self.lastLeftViewWidth;
    [expandMainAnimationDict setObject:[NSValue valueWithRect:newMainFrame] forKey:NSViewAnimationEndFrameKey];

    NSMutableDictionary *expandInspectorAnimationDict = [NSMutableDictionary dictionaryWithCapacity:2];
    [expandInspectorAnimationDict setObject:left forKey:NSViewAnimationTargetKey];
    NSRect newInspectorFrame = left.frame;
    newInspectorFrame.size.width = self.lastLeftViewWidth;
    newInspectorFrame.origin.x = self.splitView.frame.size.width-self.lastLeftViewWidth;
    [expandInspectorAnimationDict setObject:[NSValue valueWithRect:newInspectorFrame] forKey:NSViewAnimationEndFrameKey];

    NSViewAnimation *expandAnimation = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObjects:expandMainAnimationDict, expandInspectorAnimationDict, nil]];
    [expandAnimation setDuration:0.60f];
    [expandAnimation startAnimation];
    [self.splitView adjustSubviews];
    [self.splitView setNeedsDisplay:YES];
}

以下链接帮助我解决了这个问题

如何用动画展开和折叠 NSSplitView 子视图?

于 2012-06-28T08:32:22.967 回答