Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View with UIBezierPath and shadow?

I have a view in a UITableViewCell which has 3 rounded corners and a shadow. I am using UIBezierPath for rounded corners but i can't drop the shadow.

This is lines of code used:

   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)];

And this need to be the result:

enter image description here

Any ideas?

Thank you!

like image 959
Gaby Fitcal Avatar asked Nov 26 '15 11:11

Gaby Fitcal


1 Answers

SOLUTION!

    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];
like image 123
Gaby Fitcal Avatar answered Oct 01 '22 23:10

Gaby Fitcal