// // UIView+CCShadow.m // YouHuiProject // // Created by 小花 on 2018/11/20. // Copyright © 2018年 kuxuan. All rights reserved. // #import "UIView+CCShadow.h" @implementation UIView (CCShadow) - (void)addShadowWithOpacity:(float)shadowOpacity shadowRadius:(CGFloat)shadowRadius andCornerRadius:(CGFloat)cornerRadius andShadowColor:(UIColor *)color { //////// shadow ///////// CALayer *shadowLayer = [CALayer layer]; shadowLayer.frame = self.layer.frame; shadowLayer.shadowColor = color.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 ///////// self.layer.cornerRadius = cornerRadius; self.layer.masksToBounds = YES; self.layer.shouldRasterize = YES; self.layer.rasterizationScale = [UIScreen mainScreen].scale; [self.superview.layer insertSublayer:shadowLayer below:self.layer]; } @end