2012-05-01 83 views
1

我正在嘗試創建一個由兩組正方形(棋狀網格)組成的可可UI,這些正方形將在底層算法運行時呈現不同的顏色。當算法的執行結束時,UI應該能夠處理點擊,平移和其他手勢。向NSView添加子視圖以獲得像象棋的網格

我迄今爲止的層次是以下的(請檢查附於具體代碼):這是一個窗口控制器

2)拆分視圖與窗口中的主窗口

1)兩個自定義視圖,MAINVIEW和側視(每一個將持有一組平方)

3)兩個視點控制器(mainViewController和sideViewController)

我想能夠加載的平方作爲MAINVIEW的子視圖和sideView。

我想有另一個自定義視圖,說SquareView與另一個nib文件。我的問題是:

a)我該如何創建這個SquareView,以便它可以用來創建將作爲子視圖添加到mainView和sideView以形成類棋格網格的正方形?

b)如何將子視圖添加到mainView和sideView以構建兩個網格?爲了簡單起見,我們假設前面提到的每個視圖都有四個不重疊的正方形。

謝謝!


MainView.m

#import "MainView.h" 


@implementation MainView 

- (void)drawRect:(NSRect)TheRect 
{ 
    [[NSColor grayColor] set]; 
    [NSBezierPath fillRect:[self bounds]]; 
} 

SideView.m

#import "SideView.h" 


@implementation MainView 

- (void)drawRect:(NSRect)TheRect 
{ 
    [[NSColor whiteColor] set]; 
    [NSBezierPath fillRect:[self bounds]]; 
} 

MainWindowController.h

#import <Cocoa/Cocoa.h> 

@class SideViewController; 
@class MainViewController; 

@interface MainWindowController : NSWindowController 
{ 
    IBOutlet NSSplitView* oMainSplitView; 
    SideViewController* sideViewController; 
    MainViewController* mainViewController; 

} 

@end 

MainWindowController.m

#import "MainWindowController.h" 
#import "SideViewController.h" 
#import "MainViewController.h" 

@implementation MainWindowController 

- (void)windowDidLoad 
{ 

    sideViewController = [[SideViewController alloc] initWithNibName:@"SideView" bundle:nil]; 
    NSView* splitViewLeftView = [[oMainSplitView subviews] objectAtIndex:0]; 
    NSView* sideView = [sideViewController view]; 
    [sideView setFrame:[splitViewLeftView bounds]]; 
    [sideView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)]; 
    [splitViewLeftView addSubview:sideView]; 

    mainViewController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil]; 
    NSView* splitViewRightView = [[oMainSplitView subviews] objectAtIndex:1]; 
    NSView* mainView = [mainViewController view]; 
    [mainView setFrame:[splitViewRightView bounds]]; 
    [mainView setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)]; 
    [splitViewRightView addSubview:mainView]; 
}  

回答

0

a)我認爲最簡單的方法是創建一個NSBoxes矩陣,您可以在代碼或IB中執行該矩陣。矩陣中的方塊可以很容易地通過它們或訪問特定的方塊。 b)我不確定你的問題在這裏 - 你可以像你在發佈的代碼中那樣做,使用[mainView addSubview:squareMatrix];

編輯後:其實,它看起來像IB不會讓你將NSBoxes嵌入到矩陣中。在過去,我製作了一個子類NSButtonCell矩陣(允許無邊框的背景顏色),它具有可點擊的64x64單元網格,並且會隨着這些點擊而改變顏色。我不知道您的視圖中是否需要固定數量的單元格,還是需要動態更改數字?像這樣的東西可以爲你工作我認爲 - 我實際上是在代碼中創建它,因爲IB在更新很多單元時非常慢。

這是我做的。就我而言,我需要的細胞,沒有邊框,但與背景的顏色,所以我不得不繼承NSButtonCell,像這樣:

-(id)initWithRGBAlpha:(NSArray *)rgbAlpha { 
    if (self == [super init]) { 
    NSColor *color = [NSColor colorWithCalibratedRed:[[rgbAlpha objectAtIndex:0]doubleValue] 
               green:[[rgbAlpha objectAtIndex:1]doubleValue] 
               blue:[[rgbAlpha objectAtIndex:2]doubleValue] 
               alpha:[[rgbAlpha objectAtIndex:3]doubleValue]]; 
    [self setBackgroundColor:color]; 
    [self setTitle:@""]; 
    [self setBordered:NO]; 
    [self setTag:0]; 
    [self setImageScaling:3]; 
    return self; 
    }else{ 
     return nil; 
    } 
} 

-(void) setState:(NSInteger)value { 
    if (value == 1) { 
     self.backgroundColor = self.selectedColor; 
     [super setState:value]; 
    }else { 
     self.backgroundColor = self.backgroundColor; 
     [super setState:value]; 
    } 
} 


-(void) setBackgroundColor:(NSColor *)color { 
    backgroundColor = color; 
    selectedColor = [color colorWithAlphaComponent:.75]; 
} 

- (void)encodeWithCoder:(NSCoder *)encoder { 
    [super encodeWithCoder:encoder]; 
    [encoder encodeObject:self.backgroundColor forKey:@"bColor"]; 
} 

- (id)initWithCoder:(NSCoder *)decoder { 
    [super initWithCoder:decoder]; 
    self.backgroundColor = [decoder decodeObjectForKey:@"bColor"]; 
    return self; 
} 

我創建的代碼矩陣,就像這樣:

@implementation RDMatrix 

-(void) initWithParentView:(NSView *) cv { 
    NSNumber *one = [NSNumber numberWithInt:1]; 
    NSArray *colors = [NSArray arrayWithObjects:one,one,one,one,nil]; 
    RDButtonCell *theCell = [[RDButtonCell alloc ]initWithRGBAlpha:colors]; 
    [self initWithFrame:NSMakeRect(200,100,1,1) mode:2 prototype:theCell numberOfRows:64 numberOfColumns:64]; 
    [self setSelectionByRect:TRUE]; 
    [self setCellSize:NSMakeSize(8,8)]; 
    [self sizeToCells]; 
    self.target = self; 
    self.action = @selector(matrixClick:); 
    self.backgroundColor = [NSColor lightGrayColor]; 
    self.drawsBackground = TRUE; 
    self.autoresizingMask = 8; 
    self.allowsEmptySelection = TRUE; 
    [cv addSubview:self]; 
} 

-(void) matrixClick: (id) sender { 
    for (RDButtonCell *aCell in self.selectedCells){ 
     if ([self.selectedCells count] < 64) { 
      aCell.backgroundColor = [NSColor colorWithCalibratedRed:1 green:0 blue:0 alpha:1]; 
     }else{ 
     aCell.backgroundColor = [NSColor colorWithCalibratedRed:0 green:.5 blue:1 alpha:1]; 
     } 
    } 
    [self deselectAllCells]; 
} 
@end 
+0

爲什麼要使用'NSBox'比自定義視圖中的方塊更容易/更好? – trojanfoe

+0

@trojanfoe,我不知道「更好」,但我認爲在IB中做起來更容易,因爲你可以按照你想要的方式佈置它,並且NSBoxes(自定義類型的)給你兩個邊界並填充顏色。最終(對我來說),歸結爲方便(在IB中執行)與在代碼中執行它的靈活性。 – rdelmar

+0

我可能不夠清楚,我對此表示歉意。這個想法是爲每個視圖添加一個正方形網格(mainView和sideView)。算法運行時,每個正方形將呈現不同的顏色。稍後的觸控板手勢將不得不由應用程序處理。我真的不知道要走哪條路。我是否應該有一個正方形的nib文件,該文件將被加載以創建一個子視圖數組?在這種情況下,我需要另一個視圖控制器的子視圖?或者使用NSCollectionView,CALayers,NSBoxes或NSButtons更好?如果我有100多個方格呢?謝謝! – user1352042

0

你可以使其如此簡單或複雜,如你所願:簡單?在MainView的drawRect方法中做所有你想要的;複雜:嵌套NSViews(或NSCell的,或NSBox的等),並讓每一個繪製自己。

就我個人而言,我會投票保持它簡單...