2014-08-30 76 views
0

這裏的目標是在用戶看到NSImageView的頂部有一個選擇圖層。但是,我的選擇層總是以某種方式結束於「後退」。這是我從我的NSImageView子類重寫-setImage:NSImageView with CALayer on top

-(void)setImage:(NSImage *)newImage { 

    if(newImage) { 

     // compute image location in view 

     _imageLocation.x = ([self frame].size.width - [newImage size].width)/2.0; 
     _imageLocation.y = ([self frame].size.height - [newImage size].height)/2.0; 

     // call to parent 
     [super setImage:newImage]; 

     if(_pixelDataManager) { 
      _pixelDataManager = nil; 
     } 
     _pixelDataManager = [[PixelDataManager alloc] initWithImageForData:newImage]; 

     _selectionLayer = [CALayer layer]; 

     [_selectionLayer setBorderColor:[[NSColor yellowColor] CGColor]]; 

     [_selectionLayer setBorderWidth:2.0]; 

     [_selectionLayer setCornerRadius:0.0]; 

     [[self layer] addSublayer:_selectionLayer]; 

    } 
    else { 

     [super setImage:nil]; 
     _pixelDataManager = nil; 
     _imageLocation.x = 0.0; 
     _imageLocation.y = 0.0; 

    } 
} 

Observe the yellow box behind the image

這裏的重點是黃色盒子。我有一個不同的項目中的自定義NSView類似的代碼,這似乎是好的,但不明白爲什麼這不適用於NSImageView。

+1

您可以嘗試更改圖層的z順序。 http://stackoverflow.com/questions/25371109/z-index-of-image-and-separator-in-uitableviewcell/25372394#25372394 – mohacs 2014-08-30 21:04:58

+0

@mohacs,感謝您先生/女士這工作! – xBACP 2014-08-30 21:27:12

+0

不客氣。 – mohacs 2014-08-30 22:46:51

回答

0

感謝mohacs,他/她回答了這個問題。