为UIView设置阴影效果

// 阴影的颜色
imgView.layer.shadowColor = [UIColor blackColor].CGColor

// 阴影的透明度
View.layer.shadowOpacity = 0.8f;

// 阴影的圆角
View.layer.shadowRadius = 4.f;

// 阴影的偏移量
View.layer.shadowOffset = CGSizeMake(10,10);

// 即使偏移量为(0,0)时,围绕view的四周依然能看到一定阴影。
View.layer.shadowOffset = CGSizeMake(0,0);

// 阴影的路径
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(-5, -5)];
//添加直线
[path addLineToPoint:CGPointMake(ScreenWidth /2, -15)];
[path addLineToPoint:CGPointMake(ScreenWidth +5, -5)];
[path addLineToPoint:CGPointMake(ScreenWidth +15, ScreenHeight /2)];
[path addLineToPoint:CGPointMake(ScreenWidth +5, ScreenHeight +5)];
[path addLineToPoint:CGPointMake(ScreenWidth /2, ScreenHeight +15)];
[path addLineToPoint:CGPointMake(-5, ScreenHeight +5)];
[path addLineToPoint:CGPointMake(-15, ScreenHeight /2)];
[path addLineToPoint:CGPointMake(-5, -5)];
// 设置阴影路径 
View.layer.shadowPath = path.CGPath;

在通过这样的方式设置阴影时,必须把父视图的masksToBounds属性关掉,因为阴影设置的方式就是加offset给超出视图部分设置颜色来实现的,一旦不让子视图超出,阴影也就看不出了。

你可能感兴趣的:(为UIView设置阴影效果)