2013-01-08 123 views
6

喜权我喜欢的CALayer的阴影添加到图,其中阴影下被放置并认为最简单的办法是权:留下阴影和UIView的

someView.layer.shadowColor = [[UIColor blackColor] CGColor]; 
someView.layer.shadowOffset = CGSizeMake(0.0f,0.0f); 
someView.layer.shadowOpacity = 1.0f; 
someView.layer.shadowRadius = 10.0f; 
someView.layer.shadowPath = [[UIBezierPath bezierPathWithRect:someView.bounds] CGPath]; 

,但我会添加一个更大的阴影如当我增加shadowRadius时它看起来不太好。我如何制作一个看起来不错的左右影子。

回答

26

我认为10是一个相当大的阴影半径,尝试3或4代替,也混浊我通常使用0.7:

someView.layer.shadowColor = [[UIColor blackColor] CGColor]; 
someView.layer.shadowOffset = CGSizeMake(0.0f,0.0f); 
someView.layer.shadowOpacity = 0.7f; 
someView.layer.shadowRadius = 4.0f; 

如果你想只在左边和右边的阴影,然后嵌入矩形上顶部和底部,因此顶部和底部的阴影隐藏您的观点背后:

CGRect shadowRect = CGRectInset(someView.bounds, 0, 4); // inset top/bottom 
someView.layer.shadowPath = [[UIBezierPath bezierPathWithRect:shadowRect] CGPath]; 

我真的不知道,如果这就是你想要的。

+0

我已经和我的设计师这个阴影交谈是小。我需要左右不同的x偏移量。 – Zeropointer

+0

令人惊叹的答案,但家伙有人实施了这个圆角(更光滑),因为这种直肠般的阴影看起来很糟糕。任何见解?提前致谢 ! :) –

+0

使用bezierPathWithRoundedRect而不是 – progrmr

0

progrmr的回答很有帮助,欢呼!

我制作了一个滑出式菜单,我遇到了围绕我的VC阴影的问题,并且中断了导航栏。原来我不得不插入阴影层。

下面是使用SWIFT我的解决方案:

rightViewController!.view.layer.shadowOpacity = 0.8 
rightViewController!.view.layer.shadowOffset = CGSizeMake(0, 3) 
rightViewController!.view.layer.shadowRadius = 4.0 

let shadowRect: CGRect = CGRectInset(rightViewController!.view.bounds, 0, 4); // inset top/bottom 
rightViewController!.view.layer.shadowPath = UIBezierPath(rect: shadowRect).CGPath 
4

SWIFT 3.0版本

imageView.layer.shadowOpacity = 0.8 
imageView.layer.shadowOffset = CGSize(width: 0, height: 3) 
imageView.layer.shadowRadius = 4.0 

let shadowRect: CGRect = imageView.bounds.insetBy(dx: 0, dy: 4) 
imageView.layer.shadowPath = UIBezierPath(rect: shadowRect).cgPath