Какао: NSToolBar с пользовательскими представлениями. Проблемы с анимацией
Итак, я получил окно:
Window.xib
Я также получил WindowController:
WindowController.h читает:
#import <Cocoa/Cocoa.h>
@interface MainWindowController : NSWindowController
{
IBOutlet NSView *firstView;
IBOutlet NSView *secondView;
IBOutlet NSView *thirdView;
int currentViewTag;
}
-(IBAction)switchView:(id)sender;
@end
И WindowController.m читает:
#import "MainWindowController.h"
@interface MainWindowController ()
@end
@implementation MainWindowController
-(id)init
{
self = [super initWithWindowNibName:@"MainWindow"];
if (self){
// Initialization code here
}
return self;
}
//- (void)windowDidLoad {
// [super windowDidLoad];
//
// // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
//}
#pragma mark - Custom view drawing
-(NSRect)newFrameForNewContentView:(NSView *)view
{
NSWindow *window = [self window];
NSRect newFrameRect = [window frameRectForContentRect:[view frame]];
NSRect oldFrameRect = [window frame];
NSSize newSize = newFrameRect.size;
NSSize oldSize = oldFrameRect.size;
NSRect frame = [window frame];
frame.size = newSize;
frame.origin.y -= (newSize.height - oldSize.height);
return frame;
}
-(NSView *)viewForTag:(int)tag{
NSView *view = nil;
if (tag == 0) {
view = firstView;
} else if (tag == 1) {
view = secondView;
} else {
view = thirdView;
}
return view;
}
-(BOOL) validateToolbarItem:(NSToolbarItem *)item
{
if ([item tag] == currentViewTag) return NO;
else return YES;
}
-(void)awakeFromNib
{
[[self window] setContentSize:[firstView frame].size];
[[[self window] contentView]addSubview:firstView];
[[[self window] contentView]setWantsLayer:YES];
}
-(IBAction)switchView:(id)sender
{
int tag = [sender tag];
NSView *view = [self viewForTag:tag];
NSView *previousView = [self viewForTag:currentViewTag];
currentViewTag = tag;
NSRect newFrame = [self newFrameForNewContentView:view];
[NSAnimationContext beginGrouping];
if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask)
[[NSAnimationContext currentContext] setDuration:1.0];
[[[[self window]contentView]animator]replaceSubview:previousView with:view];
[[[self window]animator]setFrame:newFrame display:YES];
[NSAnimationContext endGrouping];
}
@end
У меня проблема в том, что когда я переключаю вкладки в моем приложении, пользовательское представление (и есть три разных размера) рисует по-разному каждый раз. Посмотрите на скриншоты, все цифры должны быть выровнены по центру, но иногда они совпадают, а другие нет. Кто-нибудь может увидеть, в чем моя ошибка, пожалуйста?
Я также добавлю, что все действия были правильно настроены + код работает отлично, если размер настраиваемого представления всегда одинаков.
Точка зрения, которая работает
Точка зрения, которая почти работает
Снова указывая, что в моем.xib все числа выровнены по оси 0x и 0y.
Appdelegate.h
#import <Cocoa/Cocoa.h>
@class MainWindowController;
@interface AppDelegate : NSObject <NSApplicationDelegate> {
MainWindowController *mainWindowController;
}
@end
Appdelegate.m
@interface AppDelegate ()
@property (nonatomic) IBOutlet NSWindow *window;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
-(void)awakeFromNib
{
if(!mainWindowController){
mainWindowController = [[MainWindowController alloc]init];
}
[mainWindowController showWindow:nil];
}
@end
1 ответ
В Интерфейсном Разработчике убедитесь, что отключили флажок "autoresizes subviews" для представления вашего окна по умолчанию