我正试图将PKCanvasView集成到React Native应用程序中,但当我尝试添加
canvasView
作为子视图,它不会显示。只有我直接退货,它才有效。
这是我的密码。
// BBNativePencilManager.swift
// import PencilKit;
@objc(BBNativePencilManager)
class BBNativePencilManager: RCTViewManager {
override func view() -> UIView {
// let canvasView = PKCanvasView();
// let drawing = PKDrawing()
// canvasView.drawing = drawing;
// if #available(iOS 14.0, *) {
// canvasView.drawingPolicy = .anyInput
// }
// if (theme == "dark") {
// canvasView.overrideUserInterfaceStyle = .dark;
// } else {
// canvasView.overrideUserInterfaceStyle = .light;
// }
// canvasView.isOpaque = false;
// canvasView.backgroundColor = .clear;
// canvasView.isMultipleTouchEnabled = true;
// canvasView.backgroundColor = .yellow
// canvasView.tool = PKInkingTool(.pen, color: UIColor.black, width: 5)
// canvasView.becomeFirstResponder()
// return canvasView;
// The above code works correctly
return BBNativePencilView(); // <-- this does not work
}
override static func requiresMainQueueSetup() -> Bool {
return true
}
}
和
BBNativePencilView
// BBNativePencilView.swift
import UIKit
import PencilKit
class BBNativePencilView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(canvas)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
lazy var canvas: PKCanvasView = {
let canvasView = PKCanvasView()
let drawing = PKDrawing.init()
canvasView.drawing = drawing;
if #available(iOS 14.0, *) {
canvasView.drawingPolicy = .anyInput
}
canvasView.isOpaque = false;
canvasView.backgroundColor = .clear;
canvasView.isMultipleTouchEnabled = true;
canvasView.backgroundColor = .yellow
canvasView.tool = PKInkingTool(.pen, color: UIColor.black, width: 5)
canvasView.becomeFirstResponder()
return canvasView;
}()
}
有人能向我解释一下这个问题吗?非常感谢。