Adding A Shadow To UIView

本文介绍如何为 iOS 中的 UIView 添加阴影效果,并详细解释了阴影属性的作用,包括偏移、颜色、模糊半径及不透明度等。文中提供了一个具体的实现案例,通过设置这些属性来创建一个带有定制阴影的按钮。

参考: http://nscookbook.com/2013/01/ios-programming-recipe-10-adding-a-shadow-to-uiview/

shadowOffset
is a CGSize representing how far to offset the shadow from the path.

shadowColor
is the color of the shadow. Shadow colors should always be opaque, because the opacity will be set by the shadowOpacity property. The shadowColor property is a CGColor not a UIColor.

shadowRadius
is the width of the shadow along the shadow path
he blur radius (in points) used to render the layer’s shadow

shadowOpacity
determines the opacity of the shadow.

shadowPath
is probably the most important of the properties. While a shadow can be drawn without specifying a path, for performance reasons you should always specify one. This path tells Core Animation what the opaque regions of the view are, and without it, things slow down severely! It is a CGPath, which is most easily created using UIBezierPath (iOS only)

按钮左边添加阴影
- (CGPathRef)fancyShadowForRect:(CGRect)rect
{
CGSize size = rect.size;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointZero];
[path addLineToPoint:CGPointMake(-12, 7)];
[path addLineToPoint:CGPointMake(-12, size.height - 7)];
[path addLineToPoint:CGPointMake(0, size.height - 7)];
[path closePath];
return path.CGPath;
}


plusBtn.layer.shadowOffset = CGSizeZero;
plusBtn.layer.shadowOpacity = 0.9;
plusBtn.layer.shadowColor = [[UIColor whiteColor] CGColor];
plusBtn.layer.shadowRadius = 5;
plusBtn.layer.shadowPath = [self fancyShadowForRect:plusBtn.bounds];

转载于:https://www.cnblogs.com/qike/p/5561345.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值