【问题标题】:Best practice for resize window with animation using ToolBar on Cocoa使用 Cocoa 上的 ToolBar 调整带有动画的窗口大小的最佳实践
【发布时间】:2015-11-29 23:21:36
【问题描述】:

我想知道在工具栏更改时调整窗口大小的最佳做法是什么。

当工具栏选择的选项发生变化时,我正在尝试获得这种效果(动画)。

有什么想法吗?

感谢您的帮助! :)

【问题讨论】:

    标签: cocoa resize window toolbar animated


    【解决方案1】:

    这是我常用的方法:

    点击新的工具栏项后,首先获取要添加的新子视图的框架大小,然后用动画改变窗口的框架。

    演示(只是改变高度,但你可以添加支持改变宽度):

    - (void)switchToTabView:(NSView *)settingView withAnimation:(BOOL)animation
    {
        NSView *windowView = self.window.contentView;
        for (NSView *view in windowView.subviews) {
            [view removeFromSuperview];
        }
    
        CGFloat oldHeight = windowView.frame.size.height;
    
        [windowView addSubview:settingView];
        CGFloat newHeight = settingView.frame.size.height;
    
        CGFloat delta = newHeight - oldHeight;
    
        NSPoint origin = settingView.frame.origin;
        origin.y -= delta;
        [settingView setFrameOrigin:origin];
    
        NSRect frame = self.window.frame;
        frame.size.height += delta;
        frame.origin.y -= delta;
        [self.window setFrame:frame display:YES animate:animation];
    }
    

    【讨论】:

    • 不错的解决方案。当您直接在 AppDelegate 中编程时,它可以完美运行。原来我正在使用 NSObject 作为 AppController,但我无法使用 self.window。
    • @GoKu 你在哪里调用了这个方法?
    【解决方案2】:

    @GoKu 你的回答给了我如何解决它的提示。我刚刚为我的窗口添加了一个名为“win”的新属性。

    这是我的解决方案:

    - (void)updateView:(int)tag{
        [[_ourViewController view] removeFromSuperview];
        if (tag==0) {
            self.ourViewController = [[UserView alloc] initWithNibName:@"UserView" bundle:nil];
        } else if (tag==1){
            self.ourViewController = [[ComputerView alloc] initWithNibName:@"ComputerView" bundle:nil];
        }
    
        NSView *newView = _ourViewController.view;
    
        NSRect windowRect = _win.frame;
        NSRect currentViewRect = newView.frame;
    
        windowRect.origin.y = windowRect.origin.y + (windowRect.size.height - currentViewRect.size.height);
        windowRect.size.height = currentViewRect.size.height;
        windowRect.size.width = currentViewRect.size.width;
    
        [self.win setContentView:newView];
        [self.win setFrame:windowRect display:YES animate:YES];
    }
    

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-06
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 2013-09-17
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多