CALayer's backgroundFilters doesn nothing on UIView

浪子不回头ぞ 提交于 2019-12-24 15:11:38

问题


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.



来源:https://stackoverflow.com/questions/16254444/calayers-backgroundfilters-doesn-nothing-on-uiview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!