1

I'm trying to draw different types of strokes using CGLayer in Swift, but I'm not sure how to do it. Can someone please guide me on how to achieve this?

Here's the code I have so far:

func drawStroke(_ rect: CGRect) {
    let context = UIGraphicsGetCurrentContext()
    let layer = CGLayer(context!, size: rect.size, auxiliaryInfo: nil)
    let layerContext = layer?.context

    layerContext?.setStrokeColor(UIColor.blue.cgColor)
    layerContext?.setLineWidth(3.0)
    layerContext?.strokeEllipse(in: rect)

    context?.draw(layer!, in: rect)
}

This code draws a blue ellipse with a stroke width of 3.0. However, I would like to know how to draw different types of strokes, look like

enter image description here

enter image description here

enter image description here

Here's an example of the type of stroke I'd like to create:

func drawImageStroke(_ rect: CGRect) {
    let context = UIGraphicsGetCurrentContext()
    let layer = CGLayer(context!, size: rect.size, auxiliaryInfo: nil)
    let layerContext = layer?.context

    let strock1 = UIImage(named: "Strock1")
    layerContext?.setStrokeColor(UIColor(patternImage: strock1!))
    layerContext?.setLineWidth(2.0)
    layerContext?.strokeRect(rect)

    context?.draw(layer!, in: rect)
}

This code draws a rectangle with a pattern view.

Can someone please provide examples of how to draw different types of strokes using CGLayer in Swift? Any help would be greatly appreciated. Thank you in advance!

0 Answers0