Swift 4.2 Draw Something with CGContext and Canvas View

Lets Build That App · 37K views · 2019 · 18:38

Open on YouTube

Software (other)UX & UI designtutorialdated

Why Stack Overflow recommends it

Stack Overflow users link it when answering or asking about UIKit freehand drawing, such as canvas views, tracking stroke points, undoing strokes, and porting the approach to SwiftUI.

Recommended by 3 people across 3 different months, 2019 to 2020.

What it is

An 18-minute iOS tutorial from Lets Build That App that builds a finger-drawing app by subclassing UIView as a Canvas. It uses CGContext for rendering and touchesBegan/touchesMoved to track touches, in Swift 4.2.

What people said

Recommends Ben Stoller · 2020-01 · Stack Overflow · on “Get bounds of PKDrawing / individual strokes in PencilKit”

I was was looking for the same thing and from this comment [LINK] and my own searching of the documentation, it seems what you're asking for doesn't exist. If you do discover a way to decode the opaque data please update this thread on how so. If you aren't necessarily set on PencilKit, you could manually code your drawing functions which is easier than it sounds. In this tutorial: [this video] I have timestamped where he shows that he has access to all the points on the lines he is creating. And another potential solution could be to use both, by have a mapping between your PencilKit drawing and the points found from the technique in this tutorial.

Recommends Elye · 2020-10 · Stack Overflow · on “Wrapping UIView around the SwiftUI, that the touch and drawn coordinate is now mis-aligned?”

Thanks! Work like a charm. I was following the tutorial and got to that code. [this video] . Mind explaining why?

Recommends Said Alır · 2020-12 · Stack Overflow · on “how to undo last draw stroke in imageContext”

Create a new class that extends UIView put that clear background UIView as the canvas. In canvas, you can use override func draw(_ rect: CGRect) Hold the new stack here and push every new shape and use popLast. You can follow this tutorial also. [this video]

Cites Koh · 2019-03 · Stack Overflow · on “Swift: Unable to touch canvas view on scrollView”

I am attempting to build an image editing function in my app. I followed this tutorial [LINK] to enable zooming capability and this tutorial [this video] for the drawing portion. I am implementing the drawing portion and realised that the canvas view is not detecting the touches when it is overlaid onto a scrollView . The current hierarchy is as such: scrollView -> imageView -> canvas Implementation so far: //At the viewController that implements the Canvas UIView var imageView: UIImageView! var canvas = Canvas() override func viewDidLoad() { super.viewDidLoad() let image = UIImage(named: "testImage") imageView = UIImageView(image: image) setupCanvas() } func setupCanvas() { imageView.addSubview(

Cites Edoardo · 2020-02 · Stack Overflow · on “Drawing something in CanvasView using SwiftUI”

I am wondering if it's possible drawing something with CGContext and CanvasView in SwiftUI instead using UIKit, like this tutorial [this video] . Do I have to integrate UIKit for doing that?

Cites Elye · 2020-10 · Stack Overflow · on “Is there away setting UI Graphic Context drawing attribute once, instead of on draw everytime?”

I have a Canvas UIView as below. (Following [this video] ) class Canvas: UIView { override func draw(_ rect: CGRect){ super.draw(rect) guard let context = UIGraphicsGetCurrentContext() else { return } // Other codes context.setLineWidth(10) context.setLineCap(.round) context.strokePath() } // Other codes } Looks like I can only setLineWidth and setLineCap inside the draw function. That means whenever a draw happens, they will get set again and again. I wonder, is there a way for me to set the drawing attribute once per launch and not to set over and over again?

Cites Go Fudge YourSelves · 2020-12 · Stack Overflow · on “Text String must be rendered within Text Component”

I know this question has been asked many times and i have already referred to those but my use case is little different. I need to allow user to draw on an ImageBackground so i created my own native view for it. I do not face this issue on ios . It occurs only in android . I have no comments in my code which is the major cause of issue in the app. As this works on ios and not in android so i think the issue could be from native side but i am still not able to figure out what it is Here is my native android code import com.facebook.react.uimanager.SimpleViewManager import com.facebook.react.uimanager.ThemedReactContext class CanvasManager: SimpleViewManager<CanvasView>() { override fun create