简体   繁体   中英

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:

在此输入图像描述

Any ideas?

Thank you!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM