【问题标题】:SwiftUI, multiple shapes with unified shadow?SwiftUI,具有统一阴影的多个形状?
【发布时间】:2020-09-25 18:49:47
【问题描述】:

在 SwiftUI 中有没有办法合并两个形状,以便它们投射出统一的阴影。我尝试了各种组合和修饰符,但似乎无法实现我所追求的外观。任何见解将不胜感激。

struct CardView: View {
    var body: some View {
        Group {
            RoundedRectangle(cornerRadius: 20)
                .fill(Color.orange)
                .frame(width: 200, height: 250)
                .zIndex(0)
            Circle()
                .trim(from: 0.5, to: 1)
                .fill(Color.orange)
                .frame(width: 100, height: 100)
                .offset(y: -125)
                .zIndex(1)
        }.shadow(color: Color.black, radius: 10, x: 0, y: 0)
    }
}

这是我得到的,也是我所追求的……

注意:zIndex 只是我尝试过的,即两个形状都具有相同的zIndex 等。它也是一种快速重新排序事物的方法,而无需在容器内移动形状。

【问题讨论】:

    标签: swiftui shadow shapes


    【解决方案1】:

    这是可能的解决方案。使用 Xcode 11.4 / iOS 13.4 测试

    struct CardView: View {
        var body: some View {
            VStack(spacing: 0) {
                Circle()
                    .trim(from: 0.5, to: 1)
                    .fill(Color.orange)
                    .frame(width: 100, height: 100)
                    .offset(x: 0, y: 50)
                RoundedRectangle(cornerRadius: 20)
                    .fill(Color.orange)
                    .frame(width: 200, height: 250)
            }
            .compositingGroup()
            .shadow(color: Color.primary, radius: 10, x: 0, y: 0)
        }
    }
    

    【讨论】:

      【解决方案2】:

      我认为正确的答案是@Asperi 的.compositingGroup,如本文所述 (https://developer.apple.com/documentation/swiftui/group/3284789-compositinggroup),但我想留下一个不同的工作版本,因为在某些用例中您可能无法或不想使用 @ 987654325@.

      那么我们看.background,可以复制粘贴代码在模拟器中查看:

      import SwiftUI
      
      struct AnyShapedView: View {
          var body: some View {
              ZStack {
                  RoundedRectangle(cornerRadius: 20)
                      .fill(Color.orange)
                      .frame(width: 200, height: 250)
                      .zIndex(0)
                  Circle()
                      .trim(from: 0.5, to: 1)
                      .fill(Color.orange)
                      .frame(width: 100, height: 100)
                      .offset(y: -125)
                      .zIndex(1)
              }
          }
      }
      
      struct ContentView: View {
          var body: some View {
              AnyShapedView()
              .background(
                  AnyShapedView()
                  .shadow(color: Color.black, radius: 10, x: 0, y: 0)
              )
          }
      }
      
      struct ContentView_Previews: PreviewProvider {
          static var previews: some View {
              ContentView()
          }
      }
      

      输出:

      上面的版本不是最佳实践,但是,请尝试了解形状和裁剪的工作原理,因为在某些用例中它可能会派上用场。

      【讨论】:

      • 感谢@punkbit 花时间解释和您的见解,每一个意见都有帮助,非常感谢。还有苹果文档中 compositinggroup 的链接,很有帮助,谢谢。
      • 是的,你的问题实际上对很多用例都非常有用,所以我也留下了这个例子:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-14
      • 2017-09-22
      相关资源
      最近更新 更多