在 Cocoa 上使用 ToolBar 使用动画调整大小 window 的最佳实践
Best practice for resize window with animation using ToolBar on Cocoa
我想知道当工具栏发生变化时调整 window 大小的最佳做法是什么。
我正在尝试在工具栏选择的选项更改时获得这种效果(动画)。
有什么想法吗?
感谢您的帮助! :)
这是我常用的方法:
点击新工具栏项后,首先获取要添加的新子视图的框架大小,然后用动画更改window的框架。
演示(只是改变高度,但你可以添加支持改变宽度):
- (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];
}
@GoKu 你的回答给了我一个解决方法的提示。我刚刚为我的 window 添加了一个新的 属性,名为 "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];
}
谢谢!
我想知道当工具栏发生变化时调整 window 大小的最佳做法是什么。
我正在尝试在工具栏选择的选项更改时获得这种效果(动画)。
有什么想法吗?
感谢您的帮助! :)
这是我常用的方法:
点击新工具栏项后,首先获取要添加的新子视图的框架大小,然后用动画更改window的框架。
演示(只是改变高度,但你可以添加支持改变宽度):
- (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];
}
@GoKu 你的回答给了我一个解决方法的提示。我刚刚为我的 window 添加了一个新的 属性,名为 "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];
}
谢谢!