Compositing Group —— SwiftUI中的防止子视图继承修饰符的方法

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(
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值