2016-08-12 85 views
0

我已經爲我的NSView編寫了陰影效果代碼。陰影效果無法使用Resizable NSView

[_nsview setWantsLayer:YES]; 
    _nsview.layer.masksToBounds = NO; 
    _nsview.layer.cornerRadius = 5; 
    _nsview.layer.shadowOffset = CGSizeMake(.3f, -.3f); 
    _nsview.layer.shadowRadius = 10; 
    _nsview.layer.shadowOpacity = 0.20; 
    _nsview.layer.shadowColor = [NSColor blackColor].CGColor; 

_nsview是該NSView的出口。上面的代碼完美工作,並給出陰影效果......但問題是,調整_nsview陰影后隱藏。

This is after running my sample app, shadow effect working perfectly

This is after resizing main window of sample app(_nsview has auto layout constraints, due to that it will also resize with main window)

回答

0

除非你使用一個層託管視圖(注:從層支持的觀點不同),那麼你應該假設NSView可以在後面隨時更改圖層屬性場景。陰影和變換是在開發者控制範圍之外改變的最常見的屬性。

下面是一些有用的書:

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreAnimation_guide/SettingUpLayerObjects/SettingUpLayerObjects.html

https://www.objc.io/issues/14-mac/appkit-for-uikit-developers/

1

使用NSShadow代替:

[_childView setWantsLayer:YES]; 
    _childView.layer.backgroundColor = [NSColor whiteColor].CGColor; 
    _childView.layer.cornerRadius = 5; 

    NSShadow *dropShadow = [[NSShadow alloc] init]; 
    [dropShadow setShadowColor:[NSColor colorWithWhite:0.1 alpha:0.6]]; 
    [dropShadow setShadowOffset:NSMakeSize(0, -5)]; 
    [dropShadow setShadowBlurRadius:5]; 
    _childView.shadow = dropShadow;