2015-10-26 3 views
0

alors je suis une fenêtre:Cocoa: NSToolBar avec des vues personnalisées. Questions d'animation

Window.xib

Je suis un WindowController aussi:

WindowController.h lit comme suit:

#import <Cocoa/Cocoa.h> 

@interface MainWindowController : NSWindowController 
{ 
    IBOutlet NSView *firstView; 
    IBOutlet NSView *secondView; 
    IBOutlet NSView *thirdView; 

    int currentViewTag; 
} 

-(IBAction)switchView:(id)sender; 


@end 

Et le WindowController.m lit comme suit:

#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 

Le problème que j'ai est-ce que quand je change d'onglet dans mon application, la vue personnalisée (et il y a trois tailles différentes) dessine différemment à chaque fois. Regardez les captures d'écran, tous les numéros doivent être alignés au centre, mais ils le sont parfois et d'autres non. Quelqu'un peut-il voir ce que mon erreur est s'il vous plaît?

J'ajouterai également que toutes les actions ont été correctement configurées + le code fonctionne parfaitement si la taille de la vue personnalisée est toujours la même.

The view that works

The view that almost works

Encore une fois remarquer que dans mon .xib tous les numéros sont alignés sur l'axe 0x et 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 

Répondre

-2

Dans Interface Builder, assurez-vous de désactiver les "autoresizes de" la case de la vue par défaut de la fenêtre