简体   繁体   English

如何仅在uiview的底部和右侧添加阴影?

[英]how to add shadow to only the bottom and right side of a uiview?

I am thinking about adding drop shadow to the bottom and right side of UIView, but I found all the solutions out there are adding shadows for four sides a view. 我正在考虑在UIView的底部和右侧添加阴影,但我发现所有的解决方案都为视图的四边增加了阴影。 Is there a way to work around that? 有办法解决这个问题吗?

You can use CAGradientLayer like so, 您可以像这样使用CAGradientLayer

CAGradientLayer *shadow = [CAGradientLayer layer];
shadow.frame = CGRectMake(-10, 0, 10, myView.frame.size.height);
shadow.startPoint = CGPointMake(1.0, 0.5);
shadow.endPoint = CGPointMake(0, 0.5);
shadow.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0 alpha:0.4f] CGColor], (id)[[UIColor clearColor] CGColor], nil];
[myView.layer addSublayer:shadow];

You'll have to change the frame to suit your needs. 你必须改变frame以满足你的需要。 This example displays a shadow along the height of a view on the left. 此示例沿左侧视图的高度显示阴影。 You can also change the start and end point to control the direction of the shadow. 您还可以更改起点和终点以控制阴影的方向。

I may do it by adding two UIImageViews with stretched shadow image on bottom and right side of the view. 我可以通过在视图的底部和右侧添加两个带有拉伸阴影图像的UIImageView来实现。 You don't have to cover all the view with those UIImageViews, you just clip as much as you need. 您不必使用这些UIImageViews覆盖所有视图,只需剪切所需的内容即可。 Take a look at the color blended layers of twitter on the iPhone, I believe those beautiful shadows are created by using UIImageViews. 看看iPhone上的twitter颜色混合层,我相信这些美丽的阴影是使用UIImageViews创建的。 And that saves system resources. 这节省了系统资源。 Of course you can use CALayer to create shadow, but I think it consumes more system resources to render the shadow, so CALayer is second choice for me. 当然你可以使用CALayer来创建阴影,但我认为它会消耗更多的系统资源来渲染阴影,因此CALayer是我的第二选择。

UIBezierPath *shadowPath = [UIBezierPath 
bezierPathWithRect:self.yourViewObj.bounds];
self.yourViewObj.layer.masksToBounds = NO;
self.yourViewObj.layer.shadowColor = [UIColor blackColor].CGColor;//*** color you want for shadow
self.yourViewObj.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
self.yourViewObj.layer.shadowOpacity = 0.7f;
self.yourViewObj.layer.shadowPath = shadowPath.CGPath;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM