【问题标题】:Cocoa: NSToolBar with Custom Views. Animation issuesCocoa:带有自定义视图的 NSToolBar。动画问题
【发布时间】:2015-10-26 20:10:04
【问题描述】:

所以我得到了一个窗口:

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

【问题讨论】:

    标签: cocoa user-interface custom-view nstoolbar


    【解决方案1】:

    在 Interface Builder 中,确保禁用窗口默认视图的“自动调整子视图”复选框

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多