【问题标题】:How to use ZStack in UIKit如何在 UIKit 中使用 ZStack
【发布时间】:2020-08-29 14:43:23
【问题描述】:

这是我的 SwiftUI 代码,它运行良好。 用户可以毫无问题地绘制和按下按钮。 我希望用户在任何东西、标签、按钮等上绘图。 但是我怎么能用 UIKit 做到这一点? 有什么想法吗?

(我为 swiftui 编辑了 Kavsoft 的代码铅笔包:https://kavsoft.tech/SwiftUI_2.0/Pencil_Kit/)

import SwiftUI
import PencilKit

struct ContentView: View {
    var body: some View {
        myContentView()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        myContentView()
    }
}

struct myContentView: View {

    @State var canvas = PKCanvasView()
    @State var isDraw = true
    @State var color : UIColor = .black
    @State var type : PKInkingTool.InkType = .pen
    @State var colorPicker = false
    @State var mytextSize = CGFloat(UIScreen.main.bounds.height * 0.032)
    @State var kalemlikSize = CGFloat(UIScreen.main.bounds.height * 0.038)
    @State private var selectedFrameworkIndex = 0
   var body: some View {
    
    VStack{
        HStack{ //MARK: HEADER
            Spacer()
 
            Button(action: {
                                             }) {
                                                                       Text("Title").foregroundColor(.black).font(.title)
                                                                       
                                            }
            Spacer()
        }.padding(5)
        ZStack{
            PKCanvasRepresentation(canvas: $canvas, isDraw: $isDraw,type: $type,color: $color)
            VStack(alignment: .leading, spacing: 10){
                //Spacer()
               
                   Text("Label1")
                Text("Label2").font(.title)
                
                   Button(action:{ // MARK: Şık A
                       print("button pressed")
                   
                   }){
                   
                    Text("my blue button")
                 
                    Spacer()
                   }.padding(5)
                
            
                Spacer()
                HStack(alignment:.bottom, spacing: 20){
                    Spacer().frame(width: 0.1)
                    Group{
                                
                                    Spacer()
                                    Button(action: {
                               
                                        self.canvas.isUserInteractionEnabled = true
                                        self.type = .pen
                                        self.isDraw = true
                                        self.color = .black
                                 
                                        
                                    }) {
                                        
                                        Image(systemName: "pencil.tip")
                                            .resizable().aspectRatio(contentMode: .fit).frame(height: kalemlikSize, alignment: .center).accentColor(.black)
                                    }
                                    
                                    
                                       Button(action: {
                                              
                                              
                                        self.canvas.drawing = PKDrawing()
                                        
                                          }) {
                                              
                                            Image(systemName: "trash").resizable().aspectRatio(contentMode: .fit).frame(height: mytextSize * 1.2, alignment: .bottom)
                                            
                                          }
                                    
                                    Spacer()
                        
                    }
 Spacer().frame(width: 0.1)
                    
                }
            }.padding(5)
               
                   
           }
    }
    
       
   }
        
}


struct PKCanvasRepresentation : UIViewRepresentable {
    
    
    @Binding var canvas : PKCanvasView
    @Binding var isDraw : Bool
    @Binding var type : PKInkingTool.InkType
    @Binding var color : UIColor
    
   
    
    var ink : PKInkingTool{
        
    PKInkingTool(type, color: color  )
    }
    
    let eraser = PKEraserTool(.vector)
    
    func makeUIView(context: Context) -> PKCanvasView{
        
       
        
        canvas.tool = isDraw ? ink : eraser
        
        return canvas
    }
    
    func updateUIView(_ uiView: PKCanvasView, context: Context) {
        
     
     
        uiView.tool = isDraw ? ink : eraser
    }
    

}

这是我的 SwiftUI 代码,它运行良好。 用户可以毫无问题地绘制和按下按钮。 我希望用户在任何东西、标签、按钮等上绘图。 但是我怎么能用 UIKit 做到这一点? 有什么想法吗?

【问题讨论】:

    标签: ios xcode uikit pencilkit


    【解决方案1】:

    如果您的代码运行良好,您可以将代码放入UIHostingController

    import SwiftUI
    class SwiftUI_ViewController: UIViewController {
        
        let contentView = UIHostingController(rootView: myContentView())
        override func viewDidLoad() {
            addChild(contentView)
            view.addSubview(contentView.view)
        }
    }
    

    然后像初始化任何其他UIViewController 一样初始化UIViewController。

    注意:调整Storyboard 或UIViewController 中的约束可能是必要的。

    【讨论】:

      猜你喜欢
      • 2021-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 2019-01-03
      • 1970-01-01
      • 2020-12-06
      相关资源
      最近更新 更多