SwiftUI:ViewModifier,其中内容是一个形状

Ric*_*alt 7 struct stroke swift viewmodifier

以下代码工作正常。所以真的,我很好......但我想了解 ViewModifiers......所以我的目标是将不变的东西与动态的东西分开,以创建一个.cardify()自定义修饰符来调用形状视图.

struct SetCard: View {
    
    let pips: Int
    let shape: SetCardShape
    let color: Color
    let shading: Double
    let isSelected: Bool
    
    var body: some View {
        
        ZStack {
            VStack {
                ForEach( 0..<pips ) { _ in
                    ZStack {
                        getShape(self.shape).opacity(self.shading).foregroundColor(self.color)
                        getShape(self.shape).stroke().foregroundColor(self.color)
                    }
                }
            }
            .padding() // for shape in RoundedRect
            RoundedRectangle(cornerRadius: 10).stroke(lineWidth: isSelected ? 3.0 : 1.0).foregroundColor(.orange)
        }
        .scaleEffect(isSelected ? 0.60 : 1.0 )
        .padding() // for spacing between cards 
    }
}
Run Code Online (Sandbox Code Playgroud)

同样,出于学术/学习原因,我想简化这个结构,并使用自定义修饰符将主要内容转换为标准卡片。

下面的代码块在我注释掉ViewModifier 结构中的第二contentCardify时才有效。所有使用填充形状作为点的卡片渲染都很好。需要描边形状(即未填充)的卡片需要content在我的CardifyViewModifer中的第二个工作。

第二个内容行产生错误:

Cardify.Content (aka. _ViewModifier_Content) 类型的值没有成员 foregroundColor

注释掉 .foregroundColor() 会产生错误:Cardify.Content 类型的值(又名 _ViewModifier_Content)没有成员笔画

struct SetCardWithCardify: View {
    
    let pips: Int
    let shape: SetCardShape
    let color: Color
    let shading: Double
    let isSelected: Bool
    
    var body: some View {
        ZStack {
            getShape(shape)
            .modifier(Cardify(pips: pips, shape: shape, color: color, shading: shading, isSelected: isSelected))
        }
    .scaleEffect(isSelected ? 0.60 : 1.0 )
        .padding() // for spacing between cards 
    }
}


struct Cardify: ViewModifier {
    
    var pips: Int
    var shape: SetCardShape
    var color: Color
    var shading: Double
    var isSelected: Bool
    
    func body(content: Content)  -> some View {
    
        ZStack {
            VStack {
                ForEach( 0..<pips ) { _ in
                    ZStack {
                        content.opacity(self.shading).foregroundColor(self.color)
                        content.stroke().foregroundColor(self.color)
                    }
                }
            }
            .padding() // for shape in RoundedRect
            RoundedRectangle(cornerRadius: 10).stroke(lineWidth: isSelected ? 3.0 : 1.0).foregroundColor(.orange)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以防万一,这是很重要的,下面的代码是的来源getShape()是源contentCardifyViewModifier。

func getShape(_ shape: SetCardShape ) -> some Shape {
    switch shape {
    case .circle:
        return AnyShape( Circle() )
    case .diamond:
        return AnyShape( SetDiamond() )
    case .squiggle:
        return AnyShape( SetSquiggle() )
    }
}


struct AnyShape: Shape {
    
    func path(in rect: CGRect) -> Path {
        return _path(rect)
    }
    
    init<S: Shape>(_ wrapped: S) {
        _path = { rect in
            let path = wrapped.path(in: rect)
            return path
        }
    }
    private let _path: (CGRect) -> Path
}
Run Code Online (Sandbox Code Playgroud)

Diamond() 和 Squiggle() 是符合 Shape 协议的结构,在这些结构中从 `func path(in rect: CGRect) -> Path 正确返回路径。

我试过用以下方法降低第二个内容行:

(content as! Shape).blah blah blah 产生错误:

Protocol Shape 只能用作通用约束,因为它具有 Self 或相关的类型要求。

我也试过:

(content as! Path)

这不会产生任何编译时错误,但在执行时失败并显示错误:

运行时遇到问题,请检查您的代码是否有围绕这一行的错误。(表示第二个内容行)。

我该怎么做才能让编译器知道内容的类型,以便 stroke() 和 foregroundColor() 可以工作?

Wil*_*ler 4

我不确定你可以,但你可以使用 Shape 上的扩展来近似它:

extension Shape {
    func modified() -> some View {
        foregroundColor(Color.secondary.opacity(0.5))
    }
}

// Usage example
struct ContentView: View {
    var body: some View {
        VStack {
            Rectangle().modified()
            Capsule().modified()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,与视图修改器不同,您无法访问环境,因此它受到一定的限制。