gpt4 book ai didi

ios - 在没有 PKCanvasView 的情况下使用 PKToolPicker

转载 作者:行者123 更新时间:2023-12-01 21:30:37 27 4
gpt4 key购买 nike

我正在尝试使用 PKToolPicker来自 PencilKit (iOS/Swift) 来自自定义 View (不是 PKCanvasView)。自定义 View 符合 PKToolPickerObserver .在编译期间一切正常,但我从来没有看到 PKToolPicker!如果我用 PKCanvasView 替换我的自定义 View ,一切正常!
我在 SwiftUI 中使用 UIViewRepresentable 执行此操作(因此 First Responder 似乎是个谜!)。
这是有问题的 SwiftUI View :

struct PencilKitView: UIViewRepresentable {
typealias UIViewType = myView

let coordinator = Coordinator()

class Coordinator: NSObject, PKToolPickerObserver {

func toolPickerSelectedToolDidChange(_ toolPicker: PKToolPicker) {
// some code
}
func toolPickerVisibilityDidChange(_ toolPicker: PKToolPicker) {
// some code
}

}
func makeCoordinator() -> PencilKitView.Coordinator {
return Coordinator()
}

func makeUIView(context: Context) -> myView {
let canvasView = myView()

canvasView.isOpaque = false
canvasView.backgroundColor = UIColor.clear
canvasView.becomeFirstResponder()

if let window = UIApplication.shared.windows.filter({$0.isKeyWindow}).first,
let toolPicker = PKToolPicker.shared(for: window) {
toolPicker.addObserver(canvasView)
toolPicker.addObserver(coordinator)
toolPicker.setVisible(true, forFirstResponder: canvasView)
}
return canvasView
}

func updateUIView(_ uiView: myView, context: Context) {

}
}

如果我替换 myViewPKCanvasView上面,将看到 PKToolPicker。
为了完整起见,这里是 MyView stub :
class myView: UIScrollView, PKToolPickerObserver {
override init(frame: CGRect) {
super.init(frame: frame)
}

required init?(coder: NSCoder) {
super.init(coder: coder)
}

public func toolPickerVisibilityDidChange(_ toolPicker: PKToolPicker) {
/// some code
}

public func toolPickerSelectedToolDidChange(_ toolPicker: PKToolPicker) {
/// some code
}

public func toolPickerIsRulerActiveDidChange(_ toolPicker: PKToolPicker) {
/// some code
}

public func toolPickerFramesObscuredDidChange(_ toolPicker: PKToolPicker) {
/// some code
}
}
有人成功地做到了这一点吗?采用 PKToolPicker 是否有一些未记录的要求?

最佳答案

这是在 SwiftUI 中显示任何自定义 UIView 的 PKToolPicker 的最简单演示。
使用 Xcode 11.4/iOS 13.4 测试
demo

struct ToolPickerDemo: View {
@State private var showPicker = false
var body: some View {
Button("Picker") { self.showPicker.toggle() }
.background(ToolPickerHelper(isActive: $showPicker))
}
}

class PickerHelperView: UIView {
override var canBecomeFirstResponder: Bool { true }
}

struct ToolPickerHelper: UIViewRepresentable {
@Binding var isActive: Bool

func makeUIView(context: Context) -> PickerHelperView {
PickerHelperView()
}

func updateUIView(_ uiView: PickerHelperView, context: Context) {
guard let window = uiView.window else { return }

let picker = PKToolPicker.shared(for: window)
picker?.setVisible(isActive, forFirstResponder: uiView)

DispatchQueue.main.async {
uiView.becomeFirstResponder()
}
}
}

关于ios - 在没有 PKCanvasView 的情况下使用 PKToolPicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63033787/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com