//阴影视图
self.viewShadow = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
self.viewShadow.backgroundColor = [UIColor redColor];
self.viewShadow.layer.shadowOpacity = 1;
self.viewShadow.layer.cornerRadius = 5;
self.viewShadow.layer.borderWidth = 1;
self.viewShadow.layer.masksToBounds = YES;
//阴影视图背景
self.viewShadowBg = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
self.viewShadowBg.layer.shadowOpacity = 1;
self.viewShadowBg.layer.shadowOffset = CGSizeZero;
self.viewShadowBg.layer.shadowColor = [UIColor greenColor].CGColor;
self.viewShadowBg.layer.shadowRadius = 10;
self.viewShadowBg.layer.borderWidth = 1;
self.viewShadowBg.layer.borderColor = [UIColor grayColor].CGColor;
self.viewShadowBg.layer.cornerRadius = 5;
自定义视图阴影,我们用到的是这个属性:view.layer.shadowPath
,我们通过这个属性来自定义视图阴影。
这里我们还用到了贝塞尔曲线,通过数学方式来描述图形效果,常见于计算机中图像的描述。objective-c中通过UIBezierPath来描述贝塞尔曲线。贝塞尔曲线知识点补充:
Bezier曲线简介
贝塞尔曲线-维基百科
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 100);
view.center = self.view.center;
view.backgroundColor = [UIColor clearColor];
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOpacity = 0.7f;
view.layer.shadowOffset = CGSizeMake(30.0f, 10.0f);
view.layer.shadowRadius = 2.0f;
view.layer.masksToBounds = NO;
//正常矩形
UIBezierPath *path = [UIBezierPath bezierPathWithRect:view.bounds];
view.layer.shadowPath = path.CGPath;
[self.view addSubview:view];
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 100);
view.center = self.view.center;
view.backgroundColor = [UIColor clearColor];
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOpacity = 0.7f;
view.layer.shadowOffset = CGSizeMake(30.0f, 10.0f);
view.layer.shadowRadius = 2.0f;
view.layer.masksToBounds = NO;
//自定义画梯形
CGSize size = view.bounds.size;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(size.width * 0.33f, size.height * 0.66f)];
[path addLineToPoint:CGPointMake(size.width * 0.66f, size.height * 0.66f)];
[path addLineToPoint:CGPointMake(size.width * 1.15f, size.height * 1.15f)];
[path addLineToPoint:CGPointMake(size.width * -0.15f, size.height * 1.15f)];
view.layer.shadowPath = path.CGPath;
[self.view addSubview:view];
UIView *view = [[UIView alloc] init];
view.frame = CGRectMake(100, 100, 100, 100);
view.center = self.view.center;
view.backgroundColor = [UIColor clearColor];
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOpacity = 0.7f;
view.layer.shadowOffset = CGSizeMake(30.0f, 10.0f);
view.layer.shadowRadius = 2.0f;
view.layer.masksToBounds = NO;
//自定义大小椭圆形
CGRect ovalRect = CGRectMake(0, 0, 100, 20);
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:ovalRect];
view.layer.shadowPath = path.CGPath;
[self.view addSubview:view];
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/daiyibo123/article/details/47345555