前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS 圆角和阴影并存的方法

iOS 圆角和阴影并存的方法

作者头像
freesan44
发布2019-04-29 18:32:57
3.6K0
发布2019-04-29 18:32:57
举报
文章被收录于专栏:freesan44freesan44

前提

圆角和阴影无法共存的原因就是因为这句代码。

Because shadow is an effect done outside the View, and that masksToBounds set to YES will tell the UIView not to draw everything that is outside itself.

这句话的意思就是,圆角都是我给你割出来的,圆角外面的阴影自然也割掉了~ 所以,这么看来,圆角与阴影不能并存啊(仅限这种圆角实现的方式)

处理方式

  1. 在下面再加一个subView负责处理圆角,而父类view处理阴影 父类View:
代码语言:javascript
复制
    NSInteger standard = 1;
    parentView.layer.cornerRadius = 10*standard;
    parentView.layer.shadowColor = [UIColor darkGrayColor].CGColor;
    // 设置阴影偏移量
    parentView.layer.shadowOffset = CGSizeMake(3*standard,3*standard);
    // 设置阴影透明度
    parentView.layer.shadowOpacity = 1;
    // 设置阴影半径
    parentView.layer.shadowRadius = 3* standard;
    parentView.layer.masksToBounds = NO;

subView:

代码语言:javascript
复制
    NSInteger standard = 1;
    subView.layer.cornerRadius = 10*standard;
    subView.layer.masksToBounds = YES;

注意父类View的masksToBounds=NO cornerRadius等于subView的大小 suvBiew的masksToBounds=YES

  1. 添加一个上层Layer:
代码语言:javascript
复制
 CALayer *subLayer=[CALayer layer]; 
CGRect fixframe = _tableView.frame; 
subLayer.frame= fixframe; 
subLayer.cornerRadius=8; 
subLayer.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.8].CGColor; 
subLayer.masksToBounds=NO; 
subLayer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色 
subLayer.shadowOffset = CGSizeMake(3,2);//shadowOffset阴影偏移,x向右偏移3,y向下偏移2,默认(0, -3),这个跟shadowRadius配合使用 
subLayer.shadowOpacity = 0.8;//阴影透明度,默认0 
subLayer.shadowRadius = 4;//阴影半径,默认3 
[self.bkgView.layer insertSublayer:subLayer below:_tableView.layer]; 
代码语言:javascript
复制
- (void)drawRect:(CGRect)rect {
    CGContextRef ref = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ref, self.bounds.origin.x+10, 0); // 起点
    CGContextAddLineToPoint(ref, self.bounds.origin.x, 10); // 连线
    CGContextAddLineToPoint(ref, 20, 10); // 连线
    CGContextClosePath(ref); // 闭拢
    [THEME_COLOR setFill];// 背景填充颜色
    [THEME_COLOR setStroke]; // 连线颜色 即边框颜色
    CGContextDrawPath(ref, kCGPathFillStroke);
}

参考 https://blog.csdn.net/feosun/article/details/86657330 https://www.jianshu.com/p/0be78fc8022d

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.04.19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前提
  • 处理方式
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档