使用UIBezierPath和阴影查看?

时间:2015-11-26 11:28:54

标签: ios objective-c iphone shadow uibezierpath

我在UITableViewCell中有一个视图,它有3个圆角和一个阴影。我正在使用UIBezierPath作为圆角,但我不能放弃阴影。

这是使用的代码行:

   CGRect bounds2 = cell.backgroundMessageView.bounds;
   UIBezierPath *maskPath2 = [UIBezierPath bezierPathWithRoundedRect:bounds2
                                                               byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomRight
                                                                     cornerRadii:CGSizeMake(5, 5)];

   CAShapeLayer *maskLayer2 = [CAShapeLayer layer];
   maskLayer2.frame = bounds2;
   maskLayer2.path = maskPath2.CGPath;


   cell.backgroundMessageView.layer.mask = maskLayer2;

    //drop shadow   

   [cell.backgroundMessageView.layer setShadowColor:[UIColor blackColor].CGColor];
   [cell.backgroundMessageView.layer setShadowOpacity:1];
   [cell.backgroundMessageView.layer setShadowRadius:3.0];
   [cell.backgroundMessageView.layer setShadowOffset:CGSizeMake(2, 2)];

这需要结果:

enter image description here

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:8)

<强>解<!/强>

    cell.backgroundMessageView.layer.cornerRadius=5;
    CGRect bounds2 = cell.backgroundMessageView.bounds;
    UIBezierPath *maskPath2 = [UIBezierPath bezierPathWithRoundedRect:bounds2
                                                                    byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomRight
                                                                          cornerRadii:CGSizeMake(5, 5)];

    CAShapeLayer *maskLayer2 = [CAShapeLayer layer];
    maskLayer2.frame = bounds2;
    maskLayer2.path = maskPath2.CGPath;
    maskLayer2.shadowRadius = 2;
    maskLayer2.shadowOpacity = 0.1;

    maskLayer2.shadowColor =[UIColor blackColor].CGColor;
    maskLayer2.fillColor = [UIColor colorWithRed:252/256.0 green:252/256.0 blue:252/256.0 alpha:1].CGColor;
    maskLayer2.shadowOffset = CGSizeMake(0, 1);


    [cell.backgroundMessageView.layer insertSublayer:maskLayer2 atIndex:0];