iOS--为视图添加阴影

iOS–为视图添加阴影

情况一:视图添加圆角,在添加阴影

   //阴影视图
    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];
posted @ 2015-08-08 14:59  AbeDay  阅读(683)  评论(0编辑  收藏  举报