2015-10-26 72 views
0

所以我得到了一個窗口:可可:帶自定義視圖的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 

問題我有是當我切換我的應用程序中的標籤時,自定義視圖(並且有三種不同大小)每次繪製不同。看截圖,所有的數字應該中心對齊,但他們有時和其他人不是。任何人都可以看到我的錯誤是什麼請嗎?

我還會補充說明,所有的操作都已經被正確配置,如果自定義視圖的大小始終相同,那麼代碼完美的工作。

The view that works

The view that almost works

再次指出的是,在我的.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 

回答

-2

在Interface Builder中,確保禁用 「autoresizes子視圖」 你的窗口的默認視圖的複選框