繁体   English   中英

如何让我的 CustomView 返回视图以及 SwiftUI 中的更多额外数据?

[英]How can I make my CustomView returns View plus some more extra data in SwiftUI?

我想构建一个CustomView ,它的功能几乎与GeometryReader一样,我不想重新构建现有的 GeometryReader,我想用它来展示我的目标,例如我创建了这个读取内容大小的CustomView ,我希望我的 CustomView 能够以我们在 Swift 或 SwiftUI 中经常看到的关闭形式发回读取的大小值,

我的目标:我正在尝试接收已在CustomView中读取并保存在我的parent/ContentView视图中的sizeOfText作为关闭形式的视图大小。

Ps:我对 Binding 或使用 ObservableObject 对此问题不感兴趣,该问题尝试以将数据作为闭包形式发送回的方式找到答案。


import SwiftUI

struct ContentView: View {

    var body: some View {

        CustomView { size in                    // <<: Here
            
            Text("Hello, world!")
                .background(Color.yellow)
            .onAppear() {
                print("read size is:", size.debugDescription)
            }
            .onChange(of: size) { newValue in
                print("read size is:", newValue.debugDescription)
            }
  
        }

    }
}

struct CustomView<Content: View>: View {
    
    @State private var sizeOfText: CGSize = CGSize()
    
    var content: () -> Content
    
    var body: some View {
        
        return content()
            .background(
                GeometryReader { geometry in
                    Color.clear.onAppear() { sizeOfText = geometry.size }
                    
                })
        
    }
    
}

将内容类型指定为CGSize ,然后将sizeOfText传递给内容。

如果您想了解更多关于关闭的信息,请访问 swift 文档。

https://docs.swift.org/swift-book/LanguageGuide/Closures.html

import SwiftUI
struct CustomView<Content: View>: View {
    
    @State private var sizeOfText: CGSize = CGSize()
    
    var content: (CGSize) -> Content
    
    var body: some View {
        return content(sizeOfText)
            .background(
                GeometryReader { geometry in
                    Color.clear.onAppear() { sizeOfText = geometry.size } 
                })
    }
}


struct ContentView: View {
    var body: some View {
        CustomView { size in
            Text("Hello, world!")
                .background(Color.yellow)
                .onAppear() {
                    print("read size is:", size.debugDescription)
                }
        }
    }
}

您可以像这样在内容闭包中指定类型: var content: (_ size: CGFloat) -> Content

然后你可以用你想要的值调用闭包。 该值也可以是 CustomView 中的@State。

struct ContentView1: View {
    var body: some View {
        CustomView { size in // <-- Here
            Text("Hello, world!")
                .background(Color.yellow)
                .onAppear() {
                    // print("read size is:", size.debugDescription)
                }
        }
    }
}

struct CustomView<Content: View>: View {
    @State private var sizeOfText: CGSize = CGSize()
    var content: (_ size: CGFloat) -> Content // <-- Here
    
    var body: some View {
        return content(10)
            .background(
                GeometryReader { geometry in
                    Color.clear.onAppear() { sizeOfText = geometry.size }
                    
                })
    }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM