I'm creating a drawing app using UIPencilKit, I also need to draw some pre-defined shapes like straight line, circle, rectangle, arrow etc. by selecting a shape from list and draw it on canvas automatically. Any help how to achieve it? Thanks In Advance
You can modify the strokes array of the PKDrawing like this:
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)
}
}