使用 UIPencilKit 的预定义形状

Predefined Shapes using UIPencilKit

我正在使用 UIPencilKit 创建一个绘图应用程序,我还需要通过从列表中选择一个形状并将其绘制到 canvas 自动地。 任何帮助如何实现它? 提前致谢

你可以这样修改PKDrawing的笔画数组:

class ViewController: UIViewController {

    @IBOutlet weak var canvasView: PKCanvasView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        drawLine(point1: CGPoint(x: 0, y: 0), point2: CGPoint(x: 1000, y: 1000), color: .red, size: CGSize(width: 10, height: 10))
    }
    
    func drawLine(point1: CGPoint, point2: CGPoint, color: UIColor, size: CGSize) {
        // Define soints on strokePath
        let strokePoint1 = PKStrokePoint(location: point1, timeOffset: TimeInterval.init(), size: size, opacity: 2, force: 2, azimuth: 2, altitude: 1)
        let strokePoint2 = PKStrokePoint(location: point2, timeOffset: TimeInterval.init(), size: size, opacity: 2, force: 2, azimuth: 2, altitude: 1)
        
        // Define strokePath
        let strokePath = PKStrokePath(controlPoints: [strokePoint1, strokePoint2], creationDate: Date())
        
        // Define stroke
        let stroke = PKStroke(ink: PKInk(.pen, color: .red), path: strokePath)
        
        // Append stroke to strokes array in drawing
        canvasView.drawing.strokes.append(stroke)
    }
}