如何向CALayer添加遮罩自定义内容的阴影

时间:2014-05-08 07:43:12

标签: ios objective-c uiview calayer

我正在尝试为蒙版UIView(来自UIImage)添加阴影,但我相信因为视图被屏蔽到边界,masksToBounds = YES阴影不是展示。

如果确实如此,那么如何在蒙面图像周围画一个阴影呢?

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


}

1 个答案:

答案 0 :(得分:-2)

尝试设置遮罩的阴影属性而不是view.layer' s,就像这样

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

请注意,shadowColor属性被忽略,并且您获得了应用于视图的相同颜色。