简体   繁体   English

如何在 SwiftUI 中返回一些 InsettableShape 协议

[英]How can I return some InsettableShape Protocol in SwiftUI

I have a function which returns some InsettableShape like this:我有一个 function 返回一些 InsettableShape是这样的:

func test(value: Int) -> some InsettableShape {
    
    if (value == 0) {
        return Circle()
    }
    else if (value == 1) {
        return Capsule()
    }
    else {
        return Rectangle()
    }
    
}

Xcode give an error of: Xcode报错:

Function declares an opaque return type, but the return statements in its body do not have matching underlying types Function 声明了一个不透明的返回类型,但其主体中的返回语句没有匹配的底层类型

because there is a missing ShapeBuilder or InsettableShapeBuilder wrapper, I cannot make this code work, how can I solve this problem?因为缺少 ShapeBuilder 或 InsettableShapeBuilder 包装器,我无法使这段代码工作,我该如何解决这个问题?

Ok, the task is to convert all those different value-types to one single, so we need a non-generic type wrapper confirming to InsettableShape protocol.好的,任务是将所有这些不同的值类型转换为一个值类型,因此我们需要一个确认InsettableShape协议的非泛型类型包装器。

Here is a simplified demo of possible approach.这是可能方法的简化演示。 Tested with Xcode 13.2 / iOS 15.2用 Xcode 13.2 / iOS 15.2 测试

演示

struct Demo_Previews: PreviewProvider {
    static var previews: some View {
        test(value: 0)
            .strokeBorder(Color.red, style: StrokeStyle(lineWidth: 50.0, lineCap: .round, lineJoin: .round))
    }
}

func test(value: Int) -> some InsettableShape {
    if (value == 0) {
        return MyInsettableShape { Circle().trim(from: 0, to: 0.5).path(in: $0) }
    }
    else if (value == 1) {
        return MyInsettableShape { Capsule().path(in: $0) }
    }
    else {
        return MyInsettableShape { Rectangle().path(in: $0) }
    }
}

// Shape independent wrapper - possible, because any shape
// can provide a path for specified region rectangle
struct MyInsettableShape: InsettableShape {
    
    private let base: (CGRect) -> Path
    private let inset: CGFloat
    
    init(base: @escaping (CGRect) -> Path, inset: CGFloat = 0) {
        self.base = base
        self.inset = inset
    }
    
    func inset(by amount: CGFloat) -> Self {
        .init(base: base, inset: amount)
    }
    
    func path(in rect: CGRect) -> Path {
        base(rect.inset(by: .init(top: inset, left: inset, bottom: inset, right: inset)))
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何返回实现另一个协议的泛型类型 - How can I return a generic type that implements another protocol 如何添加 SwiftUI 某些视图类型作为协议的要求? - How to add a SwiftUI some View Type as a Requirement to an Protocol? 如何在 SwiftUI 中制作具有某种风格的文本编辑器? - How can I make a text editor with some style in SwiftUI? 如何使我的 CustomView 符合 SwiftUI 中的多个协议? - How can I make my CustomView conform to more than one protocol in SwiftUI? 使用 SwiftUI 中的协议来提供“一些视图”/Generics? - Using protocol in SwiftUI for providing "some View" / Generics? Swift:如何使具有子类返回类型的函数符合协议,其中超类被定义为返回类型? - Swift: How can I make a function with a Subclass return type conform to a protocol, where a Superclass is defined as a return type? 如何在 SwiftUI 中滚动到顶部 - How can I scrollToTop in SwiftUI 如何让我的 CustomView 返回视图以及 SwiftUI 中的更多额外数据? - How can I make my CustomView returns View plus some more extra data in SwiftUI? 如何制作返回一些视图并接受 SwiftUI 中的闭包的 function? - How can I make a function which returns some View and accepts a closure in SwiftUI? SwiftUI:当我从另一个初始化程序调用它时,如何让我的闭包返回“内容”而不是“一些视图”? - SwiftUI: How do I make my closure return 'Content' instead of 'some View' when calling it from another initialiser?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM