博客> 给UIView添加阴影效果
给UIView添加阴影效果
1小时前 评论:0 阅读:18 进击的可乐SAMA

1.添加普通阴影效果


UIView * shadowView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
shadowView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:shadowView];

//设置阴影的颜色
shadowView.layer.shadowColor = [UIColor blackColor].CGColor;
//设置阴影的透明度 默认为0
shadowView.layer.shadowOpacity = 0.6;
//阴影的偏移(默认为0,-3)即默认为向上偏移3
shadowView.layer.shadowOffset = CGSizeMake(4, 4);
//阴影半径(与阴影的偏移配合使用)
shadowView.layer.shadowRadius = 4;

2.添加路径阴影效果


UIView * shadowView2 = [[UIView alloc] initWithFrame:CGRectMake(100, 300, 100, 100)];
shadowView2.backgroundColor = [UIColor whiteColor];
[self.view addSubview:shadowView2];

//阴影的基本设置
shadowView2.layer.shadowColor = [UIColor blackColor].CGColor;
shadowView2.layer.shadowOffset = CGSizeMake(0, 0);
shadowView2.layer.shadowOpacity = 0.6;
shadowView2.layer.shadowRadius = 5;

//设置路径
UIBezierPath * path = [UIBezierPath bezierPath];
float width = shadowView2.bounds.size.width;
float height = shadowView2.bounds.size.height;
float x = shadowView2.bounds.origin.x;
float y = shadowView2.bounds.origin.y;
float addWH = 10;

CGPoint topLeft = CGPointMake(x - addWH, y - addWH);
CGPoint topRight = CGPointMake(x + addWH + width, y - addWH);
CGPoint bottomLeft = CGPointMake(x - addWH, y + height + addWH);
CGPoint bottomRight = CGPointMake(x + width + addWH, y + height + addWH);

CGPoint topMiddle = CGPointMake(x + width/2, y - addWH * 2);
CGPoint leftMiddle = CGPointMake(x - addWH * 2, y + height / 2);
CGPoint bottomMiddle = CGPointMake(x + width / 2, y + height + addWH * 2);
CGPoint rightMiddle = CGPointMake(x + width + addWH * 2, y + height / 2);

//连接路径
[path moveToPoint:topLeft];
[path addQuadCurveToPoint:topRight controlPoint:topMiddle];
[path addQuadCurveToPoint:bottomRight controlPoint:rightMiddle];
[path addQuadCurveToPoint:bottomLeft controlPoint:bottomMiddle];
[path addQuadCurveToPoint:topLeft controlPoint:leftMiddle];

//设置阴影路径
shadowView2.layer.shadowPath = path.CGPath;
收藏
0
sina weixin mail 回到顶部