简体   繁体   中英

How to add a shadow to CALayer that is masked to custom content

I'm trying to add a shadow to a masked UIView (from a UIImage ), but I believe because the view is masked to bounds, masksToBounds = YES the shadow isn't showing.

If this is indeed the case, how then can I accomplish drawing a shadow around a masked image?

-(UIView *)modifyViewWithMask:(UIView *)view
{
    view.alpha = 0.5;
    UIView *blackView = [[UIView alloc] initWithFrame:view.bounds];
    blackView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    blackView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    [view addSubview:blackView];


    UIImage* bubbleImage = [UIImage imageNamed:@"Bubble"];

    CALayer* mask = [CALayer layer];
    mask.contents = (id)[bubbleImage CGImage];
    mask.frame = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height);
    view.layer.mask = mask;
    view.layer.masksToBounds = YES;


    view.layer.shadowColor = [UIColor blackColor].CGColor;
    view.layer.shadowOffset = CGSizeMake(10, 10);
    view.layer.shadowRadius = 10;
    view.layer.shadowOpacity = 1;


}

Try to set mask's shadow properties instead of view.layer's, like this

//mask.shadowColor = [UIColor blackColor].CGColor;
mask.shadowOffset = CGSizeMake(5, 5);
mask.shadowRadius = 2;
mask.shadowOpacity = 1;

Note that shadowColor property ignored, and you got same color that applied to view.

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