如何在 UIKit 应用程序中托管 SwiftUI 代码

问题描述 投票:0回答:1

我有一个名为 UploadViewV2 Scene 的故事板,其中包含一个 UIView“进度条”,它已作为名为“ProgressBarView”的 IBOutlet 连接到我的 swift 文件。如何在“进度条”UIView 中托管 SwiftUI 代码?当我使用 UIHostingController 的建议方式时,我的 main.m 文件中出现错误,显示“Thread 1:”[ setValue:forUndefinedKey:]: 这个类与键 ProgressBar 的键值编码不兼容。”我正在尝试使用 MikeLee 在这篇文章

中提供的信息

   import Foundation
//import UIKit
import SwiftUI

@objc public protocol CMWUploadViewV2Delegate: NSObjectProtocol {
    @objc func uploadOKButtonPushed()
    @objc func uploadAbortButtonPushed()
    @objc func uploadErrorLogButtonPushed()
    @objc func uploadRetryButtonPushed()
}

//does class scope need to be broadened to all of code?
@objc class CMWSTUploadViewV2Controller: UIViewController {
  
    @IBOutlet var ProgressBarView: UIView!
    
    // embed SwiftUI into the UIKit storyboard view controller
    override func viewDidLoad() {
            super.viewDidLoad()

            let childView = UIHostingController(rootView:ContentView())
            addChild(childView)
            childView.view.frame = ProgressBarView.bounds
        ProgressBarView.addSubview(childView.view)
            childView.didMove(toParent: self)
        }

        
    
    @objc weak var delegate: CMWUploadViewV2Delegate?
    
}
    struct ContentView: View {
        @State var progressValue: Float = 0.3
        let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
        @State private var degress: Double = -110
        
        //mine
        let label = UILabel()
        
        
        var body: some View {
            
            VStack {
                Spacer()
                Spacer()
                Label("Resetting to upload mode.", systemImage: "clock")
                    .font(.title)
                    .foregroundColor(Color(hex: "000"))
                ZStack{
                    RoundedRectangle(cornerRadius: 25)
                        .fill(Color(hex: "494949"))
                        .frame(width: 300, height: 300)
                    
                    ProgressBar(progress: self.$progressValue)
                        .frame(width: 250.0, height: 250.0)
                        .padding(40.0).onReceive(timer) { _ in
                            withAnimation {
                                if progressValue < 0.8999996 {
                                    progressValue += 0.0275
                                }
                            }
                        }
                    
                    ProgressBarTriangle(progress: self.$progressValue).frame(width: 280.0, height: 290.0).rotationEffect(.degrees(degress), anchor: .bottom)
                        .offset(x: 0, y: -150).onReceive(timer) { input in
                            withAnimation(.linear(duration: 0.01).speed(200)) {
                                if degress < 110.0 {
                                    degress += 10
                                }
                                print(degress)
                            }
                        }
                }
                
                
                //***Label2
                
                VStack(alignment :.leading) {
                    Label("Controller type: 60PJTV4", image: "ControllerIcon")
                        .foregroundColor(Color(hex: "000"))
                    Label("Connection type: CAN", image: "CANIconSmall")
                        .foregroundColor(Color(hex: "000"))
                    Label("Status:", image: "StatusIcon")
                        .foregroundColor(Color(hex: "000"))
                    Label("Controller Responding This needs to be 4 lines.", image: "")
                        .foregroundColor(Color(hex: "000"))
                    
                    
                    
                    Spacer()
                }
            }
        }
        
        struct ProgressBar: View {
            @Binding var progress: Float
            
            var body: some View {
                VStack{
                    Label("Address: 0x1c000", systemImage: "target")
                        .foregroundColor(Color(hex: "8f8f8f"))
                ZStack {
                    
                    Circle()
                        .trim(from: 0.35, to: 0.85)
                        .stroke(style: StrokeStyle(lineWidth: 12.0, lineCap: .round, lineJoin: .round))
                        .opacity(0.3)
                        .foregroundColor(Color.gray)
                        .rotationEffect(.degrees(54.5))
                    
                    Circle()
                        .trim(from: 0.35, to: CGFloat(self.progress))
                        .stroke(style: StrokeStyle(lineWidth: 12.0, lineCap: .round, lineJoin: .round))
                        .fill(AngularGradient(gradient: Gradient(stops: [
                            .init(color: Color.init(hex: "daff00"), location: 0.39000002),
                            //.init(color: Color.init(hex: "E59148"), location: 0.48000002),
                            .init(color: Color.init(hex: "00ff04"), location: 0.5999999),
                            //.init(color: Color.init(hex: "EEED56"), location: 0.7199998),
                            .init(color: Color.init(hex: "32E1A0"), location: 0.8099997)]), center: .center))
                        .rotationEffect(.degrees(54.5))
                    
                    VStack{
                        
                        
                        Text("824").font(Font.system(size: 44)).bold().foregroundColor(Color.init(hex: "ffffff"))
                        //Text("Please standby...").bold().foregroundColor(Color.init(hex: "FF6600"))
                        
                        Label("Please standby...", systemImage: "clock")
                            .foregroundColor(Color(hex: "FF6600"))
                        Label("Uploading memory model to controller", systemImage: "")
                            .foregroundColor(Color(hex: "8f8f8f"))
                        
                    }
                    }
                    
                }
                
            }
            
        }
        
        
        struct ProgressBarTriangle: View {
            @Binding var progress: Float
            
            
            var body: some View {
                ZStack {
                    Image("triangle").resizable().frame(width: 10, height: 10, alignment: .center)
                }
            }
        }
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    
    extension Color {
        init(hex: String) {
            let hex = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted)
            var int: UInt64 = 0
            Scanner(string: hex).scanHexInt64(&int)
            let a, r, g, b: UInt64
            switch hex.count {
            case 3: // RGB (12-bit)
                (a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17)
            case 6: // RGB (24-bit)
                (a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF)
            case 8: // ARGB (32-bit)
                (a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF)
            default:
                (a, r, g, b) = (1, 1, 1, 0)
            }
            
            self.init(
                .sRGB,
                red: Double(r) / 255,
                green: Double(g) / 255,
                blue:  Double(b) / 255,
                opacity: Double(a) / 255
            )
        }
        
    }
    
swift uiview
1个回答
0
投票

错误“此类与键的键值编码不兼容...”通过转到情节提要然后连接检查器并删除先前已从 swift 文件中删除且不需要的旧插座来解决。以下是最终在 UIView 中托管 SwiftUI 代码的内容:

override func viewDidLoad() {
            super.viewDidLoad()

            let childView = UIHostingController(rootView:ContentView())
            addChild(childView)
            childView.view.frame = ProgressBarView.bounds
        ProgressBarView.addSubview(childView.view)
            childView.didMove(toParent: self)
        }
© www.soinside.com 2019 - 2024. All rights reserved.