//切圆角加阴影
- (void)addShadowToView:(UIView *)view
withOpacity:(float)shadowOpacity
withShowColor:(UIColor*)shadowColor
shadowRadius:(CGFloat)shadowRadius
andCornerRadius:(CGFloat)cornerRadius
{
//////// shadow /////////
_shadowLayer = [CALayer layer];
_shadowLayer.frame = view.layer.frame;
_shadowLayer.shadowColor = shadowColor.CGColor;//shadowColor阴影颜色
_shadowLayer.shadowOffset = CGSizeMake(0, 5);//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 removeFromSuperlayer];
[view.superview.layer insertSublayer:_shadowLayer below:view.layer];
}
网友评论