0

我想在绘图时获得苹果铅笔的值(位置、力、方位角)。我用了一个铅笔套件。我的问题是如果我启用了 PKCanvasView 的用户交互。我可以画画,但我无法获得价值。但是,如果我禁用用户交互,我就无法绘制,但我可以获得值。我该如何解决?

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Set up the canvas view with the first drawing from the data model.
    canvasView.delegate = self
    canvasView.alwaysBounceVertical = true
    
    // Set up the tool picker
    if #available(iOS 14.0, *) {
        toolPicker = PKToolPicker()
    } else {
        // Set up the tool picker, using the window of our parent because our view has not
        // been added to a window yet.
        let window = parent?.view.window
        toolPicker = PKToolPicker.shared(for: window!)
    }
    
    toolPicker.setVisible(true, forFirstResponder: canvasView)
    toolPicker.addObserver(canvasView)
    toolPicker.addObserver(self)
    canvasView.becomeFirstResponder()
}



override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    touches.forEach { (touch) in
        updateGagues(with: touch)
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    touches.forEach { (touch) in
        updateGagues(with: touch)
    }
    
}
   
private func updateGagues(with touch: UITouch) {
    txtForce = touch.force.valueFormattedForDisplay ?? ""
    
    let azimuthUnitVector = touch.azimuthUnitVector(in: canvasView)
    txtAzimuthUnitVector = azimuthUnitVector.valueFormattedForDisplay ?? ""
    
    let azimuthAngle = touch.azimuthAngle(in: canvasView)
    txtAzimuthAngle = azimuthAngle.valueFormattedForDisplay ?? ""
    
    // When using a finger, the angle is Pi/2 (1.571), representing a touch perpendicular to the device surface.
    txtAltitudeAngle = touch.altitudeAngle.valueFormattedForDisplay ?? ""
    
    let location = touch.preciseLocation(in: canvasView)
    txtLocation = location.valueFormattedForDisplay ?? ""
    
    print("Location: \(txtLocation)")
    print("Force: \(txtForce)")
    print("AzimuthUnitVector: \(txtAzimuthUnitVector)")
    print("AzimuthAngle: \(txtAzimuthAngle)")
}

在此处输入图像描述

4

0 回答 0