Compositing Group —— SwiftUI中的防止子视图继承修饰符的方法
概述:
在使用Material时,经常会遇到子视图继承了父视图阴影的情况,如下图所示,字体的边缘也是有阴影的。之前我一直用 .cornerRadius(_) 来避免这种继承发生,但是毕竟它本身是用来裁切视图的。今天突然发现一个.compositingGroup(),很好用!但是苹果文档看半天没明白(样例也太奇怪了)。所以写了这篇文章。
Apple Documentation
A compositing group makes compositing effects in this view’s ancestor views, such as opacity and the blend mode, take effect before this view is rendered.
Use compositingGroup() to apply effects to a parent view before applying effects to this view.
Compositing Group 向本视图的父视图进行复合效果,比如不透明度或者渲染模式,这些效果会在本视图渲染前生效。
在给本视图添加效果前,使用comspsitingGroup给本视图的父视图提供视图效果
In the example below the compositingGroup() modifier separates the application of effects into stages. It applies the opacity(_😃 effect to the VStack before the blur(radius:) effect is applied to the views inside the enclosed ZStack. This limits the scope of the opacity change to the outermost view.
在下面的样例中,使用compositingGroup修饰符,分阶段应用视觉效果。它首先将opacity效果应用到Vstack,之后向ZStack里面的视图添加blur的效果。这样就将不透明度的效果限制在最外面的视图
VStack {
ZStack {
Text("CompositingGr