Search code examples
iphoneuiviewshadowquartz-core

Opacity of layer's shadow


I add shadow to the UIView:

[blurView.layer setCornerRadius:kCornerRadius];    
blurView.layer.masksToBounds = NO;
blurView.layer.shadowColor = [[UIColor redColor] CGColor];
blurView.layer.backgroundColor = blurView.layer.shadowColor;
blurView.layer.shadowPath = [UIBezierPath
                             bezierPathWithRoundedRect:CGRectMake(-2.0*kCornerRadius, -2.0*kCornerRadius, blurView.bounds.size.width+4.0*kCornerRadius, blurView.bounds.size.height+4.0*kCornerRadius)
                             cornerRadius:kCornerRadius].CGPath;
blurView.layer.cornerRadius = kCornerRadius;
blurView.layer.shadowOffset = CGSizeMake(0.0, 0.0);
blurView.layer.shadowRadius = kCornerRadius;
blurView.layer.shadowOpacity = kOpacity;
blurView.layer.opacity = 1.0;

This part of code gives me a this view, like a uiview is blured:

enter image description here

When I try change opacity of blurView.layer (for example to 0.5), I get unexpected view:

enter image description here

As you can see - I get a sharp edges of uiview.

I think, shadow opacity is a part of opacity of layer - it's a cause of this 'error'. Can anybody help me to fix this? May be can I merge layers before change opacity of blurView.layer or something like this?

UPD

With this fix: blurView.layer.shadowColor = [[UIColor colorWithRed:1 green:0 blue:0 alpha:0.5] CGColor];

I get this:

enter image description here

UPD

Answer is using setShouldRasterize method:

enter image description here

Thanks everybody!

[blurView.layer setShouldRasterize:YES];

Solution

  • The answer is using this code line:

    blurView.layer.shouldRasterize = YES;