如何在调整大小时动画化NSSplitView? [英] How to animate the NSSplitView while resizing?

查看:156
本文介绍了如何在调整大小时动画化NSSplitView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以动画的方式调整 NSSplitView 的程序。
下面是在 SplitView中调整视图大小的代码



在awakeFromNib中:Iam观察 NSSplitViewWillResizeSubviewsNotification

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



SplitView调整大小

  NSView * leftSubView = [[splitView subview] 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的大小。我应该添加animationCode(当rightView移动到replace



我有两个视图的rects在resize之前和之后。我应该写动画代码。

解决方案

Iam能够解决动画。以下代码适用于我

   - (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子视图?


I would like to animate while resizing NSSplitView programatically. Here is the code for resizing the view in SplitView

In awakeFromNib: Iam observing the NSSplitViewWillResizeSubviewsNotification

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

SplitView Resize

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

I am able to resize the splitView.Where should I add the animationCode.(As rightView moves to replace the leftSubView, I want the resize happen with some delay) ?

I have the rects of the two Views before and after the resize.Where should I write the animation Code.

解决方案

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?

这篇关于如何在调整大小时动画化NSSplitView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆