4

我正在尝试PKCanvasView在图像顶部分层,这与初始布局配合良好,但是当调整视图大小(通过旋转 iPad)时,PKDrawing内部没有缩放,导致图像注释位于不正确的位置。我正在努力弄清楚如何正确缩放PKDrawing以保留正确的位置。

显示我的初始绘图错位的示例

这是一个演示该问题的示例:

class ImageDrawingView: UIView {

    private let imageView = UIImageView(frame: .zero)
    private let drawingView = PKCanvasView(frame: .zero)
    private let toolPicker = PKToolPicker()

    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }

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

    private func commonInit() {
        // Setup image view
        addSubview(imageView)
        imageView.image = UIImage(named: "TestImage") // Here I just used a screen grab of the lock screen
        imageView.contentMode = .scaleAspectFill

        // Setup drawing view
        addSubview(drawingView)
        drawingView.isOpaque = false
        toolPicker.setVisible(true, forFirstResponder: drawingView)
        toolPicker.addObserver(drawingView)
        drawingView.becomeFirstResponder()
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        imageView.frame = bounds
        drawingView.frame = bounds
    }
}

class ViewController: UIViewController {

    private let imageDrawingView = ImageDrawingView(frame: .zero)

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(imageDrawingView)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        imageDrawingView.frame = view.bounds
    }
}

我想如果我可以计算转换,我就可以使用drawingView.drawing.transform(using: transformScale). 请问我该如何计算转换?

4

0 回答 0