将 UIView 包裹在 SwiftUI 周围,触摸和绘制的坐标现在未对齐?
Wrapping UIView around the SwiftUI, that the touch and drawn coordinate is now mis-aligned?
我有一个Canvas
观点如下
class Canvas: UIView {
override func draw(_ rect: CGRect){
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else { return }
paths.forEach { path in
switch(path.type) {
case .move:
context.move(to: path.point)
break
case .line:
context.addLine(to: path.point)
break
}
}
context.setLineWidth(10)
context.setLineCap(.round)
context.strokePath()
}
var paths = [Path]()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: nil) else { return }
paths.append(Path(type: .move, point: point))
setNeedsDisplay()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: nil) else { return }
paths.append(Path(type: .line, point: point))
setNeedsDisplay()
}
}
class Path {
let type: PathType
let point: CGPoint
init(type: PathType, point: CGPoint) {
self.type = type
self.point = point
}
enum PathType {
case move
case line
}
}
当我使用正常 ViewController、
加载它时
class ViewController: UIViewController {
let canvas = Canvas()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(canvas)
canvas.backgroundColor = .white
canvas.frame = view.frame
}
}
一切正常。我可以根据触摸发生的位置绘制,如下所示。
但是,如果我将它包装在 SwiftUI 框架中,如下所示
struct ContentView: View {
var body: some View {
CanvasSwiftUI()
}
}
struct CanvasSwiftUI : UIViewRepresentable {
func makeUIView(context: Context) -> UIView {
Canvas()
}
func updateUIView(_ uiView: UIView, context: Context) {
uiView.backgroundColor = .white
}
}
我的触摸和绘图不匹配。绘图坐标低于触摸发生的位置,如下图
在将它包裹在 SwiftUI 周围时,我是否遗漏了任何东西,触摸和绘制坐标现在未对齐?
您遇到的问题与 SwiftUI 无关,它在您的代码中
guard let point = touches.first?.location(in: nil) else { return }
你应该使用
guard let point = touches.first?.location(in: self) else { return }
我有一个Canvas
观点如下
class Canvas: UIView {
override func draw(_ rect: CGRect){
super.draw(rect)
guard let context = UIGraphicsGetCurrentContext() else { return }
paths.forEach { path in
switch(path.type) {
case .move:
context.move(to: path.point)
break
case .line:
context.addLine(to: path.point)
break
}
}
context.setLineWidth(10)
context.setLineCap(.round)
context.strokePath()
}
var paths = [Path]()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: nil) else { return }
paths.append(Path(type: .move, point: point))
setNeedsDisplay()
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: nil) else { return }
paths.append(Path(type: .line, point: point))
setNeedsDisplay()
}
}
class Path {
let type: PathType
let point: CGPoint
init(type: PathType, point: CGPoint) {
self.type = type
self.point = point
}
enum PathType {
case move
case line
}
}
当我使用正常 ViewController、
加载它时class ViewController: UIViewController {
let canvas = Canvas()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(canvas)
canvas.backgroundColor = .white
canvas.frame = view.frame
}
}
一切正常。我可以根据触摸发生的位置绘制,如下所示。
但是,如果我将它包装在 SwiftUI 框架中,如下所示
struct ContentView: View {
var body: some View {
CanvasSwiftUI()
}
}
struct CanvasSwiftUI : UIViewRepresentable {
func makeUIView(context: Context) -> UIView {
Canvas()
}
func updateUIView(_ uiView: UIView, context: Context) {
uiView.backgroundColor = .white
}
}
我的触摸和绘图不匹配。绘图坐标低于触摸发生的位置,如下图
在将它包裹在 SwiftUI 周围时,我是否遗漏了任何东西,触摸和绘制坐标现在未对齐?
您遇到的问题与 SwiftUI 无关,它在您的代码中
guard let point = touches.first?.location(in: nil) else { return }
你应该使用
guard let point = touches.first?.location(in: self) else { return }