How to animate the NSSplitView while resizing?

不羁的心 提交于 2019-12-03 21:56:22
Ram

Iam able to solve the animations. Following Code works for me

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

Following Link helped me in solving the issue

How to expand and collapse NSSplitView subviews with animation?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!