首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SwiftUI: ViewModifier,其中内容是形状

SwiftUI: ViewModifier,其中内容是形状
EN

Stack Overflow用户
提问于 2020-07-01 16:57:22
回答 1查看 1.7K关注 0票数 7

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

代码语言:javascript
复制
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 
    }
}

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

下面的代码块只在我注释掉Cardify ViewModifier结构中的第二个content行时才能工作。所有的卡片,使用填充形状的点滴渲染只是很好。需要抚摸形状的卡片(即没有填充)需要我的Cardify ViewModifer中的第二个ViewModifer才能工作。

第二个内容行产生错误:

类型Cardify.Content的值(又名。( _ViewModifier_Content)没有成员foregroundColor

注释掉.foregroundColor()将生成错误: Cardify.Content (又名)类型的值。( _ViewModifier_Content)没有成员卒中

代码语言:javascript
复制
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)
        }
    }
}

为了防止这一点很重要,下面的代码是getShape()的源,它是Cardify ViewModifier中的content的源。

代码语言:javascript
复制
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
}

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

我试着将第二条内容线降到:

(content as! Shape).blah blah blah,它生成错误:

协议形状只能用作泛型约束,因为它具有自或关联的类型要求。

我也尝试过:

(content as! Path)

这不会生成任何编译时错误,但在执行时会出现错误:

在运行过程中遇到了一个问题,请检查您的代码在这一行周围是否有错误。(指示第二内容行)。

我能做些什么来让编译器知道这是什么类型的内容,这样就可以让行程()和foregroundColor()工作了?

EN

回答 1

Stack Overflow用户

发布于 2020-09-11 01:44:10

我不确定你能不能,但是你可以用形状上的一个扩展来近似它:

代码语言:javascript
复制
extension Shape {
    func modified() -> some View {
        self
            .foregroundColor(Color.secondary.opacity(0.5))
    }
}

// Usage example
struct ContentView: View {
    var body: some View {
        VStack {
            Rectangle().modified()
            Capsule().modified()
        }
    }
}

但是,与视图修饰符不同的是,您不能访问环境,因此它是有限的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62681769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档