IOS UIView周边加阴影,并且同时圆角

热门标签

,

特别声明:文章多为网络转载,资源使用一般不提供任何帮助,特殊资源除外,如有侵权请联系!

在给view加阴影并且同时加圆角的时候,会遇到经典的masksToBounds问题,masksToBounds为YES或NO时,仅阴影或圆角生效,不能同时生效。为解决此问题,解决办法是加一层layer来实现。
要点:
1. shadow加到单独的layer上面,layer和view.layer同frame,并且是路经阴影额?,然后加到view的底层;
2. 在view的layer上面加cornerRadius。
具体如下

/*
 周边加阴影,并且同时圆角
 */
+ (void)addShadowToView:(UIView *)view
            withOpacity:(float)shadowOpacity
           shadowRadius:(CGFloat)shadowRadius
        andCornerRadius:(CGFloat)cornerRadius
{
    //////// shadow /////////
    CALayer *shadowLayer = [CALayer layer];
    shadowLayer.frame = view.layer.frame;
    
    shadowLayer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
    shadowLayer.shadowOffset = CGSizeMake(0, 0);//shadowOffset阴影偏移,默认(0, -3),这个跟shadowRadius配合使用
    shadowLayer.shadowOpacity = shadowOpacity;//0.8;//阴影透明度,默认0
    shadowLayer.shadowRadius = shadowRadius;//8;//阴影半径,默认3
    
    //路径阴影
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    float width = shadowLayer.bounds.size.width;
    float height = shadowLayer.bounds.size.height;
    float x = shadowLayer.bounds.origin.x;
    float y = shadowLayer.bounds.origin.y;
    
    CGPoint topLeft      = shadowLayer.bounds.origin;
    CGPoint topRight     = CGPointMake(x + width, y);
    CGPoint bottomRight  = CGPointMake(x + width, y + height);
    CGPoint bottomLeft   = CGPointMake(x, y + height);
    
    CGFloat offset = -1.f;
    [path moveToPoint:CGPointMake(topLeft.x - offset, topLeft.y + cornerRadius)];
    [path addArcWithCenter:CGPointMake(topLeft.x + cornerRadius, topLeft.y + cornerRadius) radius:(cornerRadius + offset) startAngle:M_PI endAngle:M_PI_2 * 3 clockwise:YES];
    [path addLineToPoint:CGPointMake(topRight.x - cornerRadius, topRight.y - offset)];
    [path addArcWithCenter:CGPointMake(topRight.x - cornerRadius, topRight.y + cornerRadius) radius:(cornerRadius + offset) startAngle:M_PI_2 * 3 endAngle:M_PI * 2 clockwise:YES];
    [path addLineToPoint:CGPointMake(bottomRight.x + offset, bottomRight.y - cornerRadius)];
    [path addArcWithCenter:CGPointMake(bottomRight.x - cornerRadius, bottomRight.y - cornerRadius) radius:(cornerRadius + offset) startAngle:0 endAngle:M_PI_2 clockwise:YES];
    [path addLineToPoint:CGPointMake(bottomLeft.x + cornerRadius, bottomLeft.y + offset)];
    [path addArcWithCenter:CGPointMake(bottomLeft.x + cornerRadius, bottomLeft.y - cornerRadius) radius:(cornerRadius + offset) startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
    [path addLineToPoint:CGPointMake(topLeft.x - offset, topLeft.y + cornerRadius)];
    
    //设置阴影路径
    shadowLayer.shadowPath = path.CGPath;
    
    //////// cornerRadius /////////
    view.layer.cornerRadius = cornerRadius;
    view.layer.masksToBounds = YES;
    view.layer.shouldRasterize = YES;
    view.layer.rasterizationScale = [UIScreen mainScreen].scale;
    
    [view.superview.layer insertSublayer:shadowLayer below:view.layer];
}

效果:
IOS UIView周边加阴影,并且同时圆角

PS:
1. label文案加阴影

- (void)addShadowForPlaceHolder
{
    if (!(_placeHolderLabel.text.length > 0)) {
        return;
    }
    
    NSShadow *shadow = [NSShadow new];
    shadow.shadowBlurRadius = 0.5f;
    shadow.shadowOffset = CGSizeMake(0.0f, 0.5f);
    shadow.shadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.4f];
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:_placeHolderLabel.text attributes:@{NSShadowAttributeName : shadow}];
    _placeHolderLabel.attributedText = attributedText;
}

2. view加普通阴影:

    _descTextView = [UITextView new];
    _descTextView.textColor = kCZUIWhiteColor;
    _descTextView.font = _fontLarge;
    _descTextView.scrollEnabled = NO;
    _descTextView.text = _liveDesc;
    _descTextView.translatesAutoresizingMaskIntoConstraints = NO;
    _descTextView.delegate = self;
    _descTextView.textContainerInset = UIEdgeInsetsZero;
    CZ_SetUIViewBackgroundColor(_descTextView, _placeHolderLabel.backgroundColor);
    _descTextView.enablesReturnKeyAutomatically = NO;
    _descTextView.layer.shadowColor = [UIColor blackColor].CGColor;
    _descTextView.layer.shadowOpacity = 0.4;
    _descTextView.layer.shadowRadius = 0.5f;
    _descTextView.layer.shadowOffset = CGSizeMake(0.0f, 0.5f);
    _descTextView.layer.shouldRasterize = YES;
    _descTextView.layer.rasterizationScale = CZ_MainScreen().scale;

3. view加普通路经阴影

- (void)addShadow:(UIView *)view
{
    view.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
    view.layer.shadowOffset = CGSizeMake(0, 0);//shadowOffset阴影偏移,默认(0, -3),这个跟shadowRadius配合使用
    view.layer.shadowOpacity = 0.3;//0.8;//阴影透明度,默认0
    view.layer.shadowRadius = 2;//8;//阴影半径,默认3
    
    //路径阴影
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    float width = view.bounds.size.width;
    float height = view.bounds.size.height;
    float x = view.bounds.origin.x;
    float y = view.bounds.origin.y;
    
    CGPoint topLeft      = view.bounds.origin;
    CGPoint topRight     = CGPointMake(x + width, y);
    CGPoint bottomRight  = CGPointMake(x + width, y + height);
    CGPoint bottomLeft   = CGPointMake(x, y + height);
    
    CGFloat offset = 0.f;
    [path moveToPoint:CGPointMake(topLeft.x - offset, topLeft.y - offset)];
    [path addLineToPoint:CGPointMake(topRight.x + offset, topRight.y - offset)];
    [path addLineToPoint:CGPointMake(bottomRight.x + offset, bottomRight.y + offset)];
    [path addLineToPoint:CGPointMake(bottomLeft.x - offset, bottomLeft.y + offset)];
    [path addLineToPoint:CGPointMake(topLeft.x - offset, topLeft.y - offset)];
    
    //设置阴影路径
    view.layer.shadowPath = path.CGPath;
}

 

标签:

未经允许不得转载:作者:SheaYang, 转载或复制请以 超链接形式 并注明出处 技术Dog|博客
原文地址:《IOS UIView周边加阴影,并且同时圆角》 发布于2019-11-21

分享到:
赞(0)

评论 抢沙发

6 + 8 =


IOS UIView周边加阴影,并且同时圆角

长按图片转发给朋友

Vieu4.0主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录