CALayer's backgroundFilters doesn nothing on U

2019-08-06 10:56发布

问题:

I am trying to add a CIFilter to the backgroundFilters property of a CALayer to let this draw in a UIView. Therefore I subclassed CALayer and in the init execute the following:

CIFilter* blur = [CIFilter filterWithName:@"CIGaussianBlur"];
[blur setDefaults];
[blur setValue:@(10) forKey:@"inputRadius"];
self.backgroundFilters = @[ blur ];

Then I created a subclass of UIView that has this custom Layer as it's layer by returning the layers class in +[UIView layerClass]

This is working, the above code is executed!

However if I place this view above a UIImageView I would expect the image to be drawn blured where I put this view. But it doesn't! The view just behaves like a regular view and takes any color / alpha value I set to it's backgroundColor property, but I just see the underlaying UIImageView as it is, without a blur!

回答1:

The reason for CALayer's backgroundFilters doing nothing on UIView is that unfortunately the backgroundFilters property is not supported on layers in iOS. It's inside inside the documentation, at the very end of the description:

Special Considerations

This property is not supported on layers in iOS.

I got bitten by this myself trying to insert a UIView that blurs the contents behind it. To blur a view on iOS, some more work seems to be required (copying the UIViews content inside an image, then blurring and showing the image).



回答2:

I think that is the expected behaviour.

From the docs:

Background filters affect the content behind the layer that shows through into the layer itself. Typically this content belongs to the superlayer that acts as the parent of the layer.

My interpretation is that these filters only affect content in the view that they are contained in.



标签: ios ios6 calayer