_dropView.layer.shadowColor=[[UIColor grayColor] colorWithAlphaComponent:0.8].CGColor;
_dropView.layer.shadowOffset=CGSizeMake(10,10);
_dropView.layer.shadowOpacity=0.5;
_dropView.layer.shadowRadius=8;
在通過這樣的方式設置陰影時,必須把父視圖的masksToBounds屬性關(guān)掉渊跋,因為陰影設置的方式就是加offset給超出視圖部分設置顏色來實現(xiàn)的,一旦不讓子視圖超出,陰影也就看不出了戒职。
圓角+陰影:
如果上面的方法一起用,把masksToBounds開了透乾,陰影無法顯示洪燥,關(guān)了的話其上的View又會遮住圓角。解決的方式只能是再加一層layer乳乌。
_dropView.backgroundColor=[[UIColor whiteColor] colorWithAlphaComponent:0.8];
_dropView.layer.cornerRadius = 8;
_dropView.layer.masksToBounds = YES;
CALayer *subLayer=[CALayer layer];
CGRect fixframe=_dropView.layer.frame;
fixframe.size.width=[UIScreen mainScreen].bounds.size.width-40;
subLayer.frame=fixframe;
subLayer.cornerRadius=8;
subLayer.backgroundColor=[[UIColor grayColor] colorWithAlphaComponent:0.5].CGColor;
subLayer.masksToBounds=NO;
subLayer.shadowColor=[UIColor grayColor].CGColor;
subLayer.shadowOffset=CGSizeMake(10,10);
subLayer.shadowOpacity=0.5;
subLayer.shadowRadius=8;
[self.layer insertSublayer:subLayer below:_dropView.layer];
swift版:
headV.layer.shadowColor = UIColor(white: 0, alpha: 0.4).cgColor
headV.layer.shadowOffset = CGSize(width: 5, height: 5)
headV.layer.shadowOpacity = 1
headV.layer.shadowRadius = 5
shadowoffset: +, + 是右下; -,+是左下; +,-是右上; -,-是左上