
//定义view的大小
let selfFrame =CGRect(x:0, y:0, width:kScreenW-75, height:kScreenW*0.93)
super .init(frame: selfFrame)
//定义view的背景颜色
backgroundColor = .white
//定义view的角度
layer.cornerRadius = 5
//定义view的阴影颜色
layer.shadowColor = UIColor.colorWidthHexString(hex: "#000000").cgColor
//阴影偏移量
layer.shadowOffset=CGSize(width:0, height:1)
//定义view的阴影宽度,模糊计算的半径
layer.shadowRadius = 10
//定义view的阴影透明度,注意:如果view没有设置背景色阴影也是不会显示的
layer.shadowOpacity = 0.1
}
Christian Brunschen在你当前的代码里,保存当前内容的GState,配置它来绘制一个阴影。然后恢复到你配置阴影之前的状态。接下来,调用drawRect:的超类实现。
绘制阴影应受阴影设置影响,需要如下之后发生:
CGContextSetShadow(currentContext, CGSizeMake(-15, 20), 5)
但是在如下之前:
CGContextRestoreGState(currentContext)
所以如果你想让超类的drawRect: 被包裹在一个阴影里,那么把你的代码写成这样怎么样?
- (void)drawRect:(CGRect)rect {
CGContextRef currentContext = UIGraphicsGetCurrentContext()
CGContextSaveGState(currentContext)
CGContextSetShadow(currentContext, CGSizeMake(-15, 20), 5)
[super drawRect: rect]
CGContextRestoreGState(currentContext)
}
ollie
迄今为止比较简单的一个方法是设置初始化视图的层属性。
self.layer.masksToBounds = NO
self.layer.cornerRadius = 8// if you like rounded corners
self.layer.shadowOffset = CGSizeMake(-15, 20)
self.layer.shadowRadius = 5
self.layer.shadowOpacity = 0.5
Z.Y.
self.layer.masksToBounds = NO
self.layer.cornerRadius = 8// if you like rounded corners
self.layer.shadowOffset = CGSizeMake(-15, 20)
self.layer.shadowRadius = 5
self.layer.shadowOpacity = 0.5
这将会减慢应用程序。只要你的视图是中规中矩的矩形,加上下面这几行代码可以提高性能。
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath
Srikar Appal
我把这个作为utils的一部分。有了这个我们不仅可以轻松地设置阴影,还可以为任何UIView获得圆角。还可以设置阴影颜色。通常选用黑色,但是当背景不是白色的时候,或许你会想用一些别的颜色。这是我的方法。
in utils.m
+ (void)roundedLayer:(CALayer *)viewLayer
radius:(float)r
shadow:(BOOL)s
{
[viewLayer setMasksToBounds:YES]
[viewLayer setCornerRadius:r]
[viewLayer setBorderColor:[RGB(180, 180, 180) CGColor]]
[viewLayer setBorderWidth:1.0f]
if(s)
{
[viewLayer setShadowColor:[RGB(0, 0, 0) CGColor]]
[viewLayer setShadowOffset:CGSizeMake(0, 0)]
[viewLayer setShadowOpacity:1]
[viewLayer setShadowRadius:2.0]
}
return
}
为了使用这个得调用[utils roundedLayer:yourview.layer radius:5.0f shadow:YES]
1.shadow
(1)
第一个属性:设置阴影透明度。
(2)
第二个属性:设置超过Bounds以外的区域不切除
(如果你之前没有对UIView设置这个属性,那么默认时NO,不需要重新设置)
2.border
borderColor:边框颜色
borderWidth:边框宽度
3.Radius
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)