Previous episode: 3. Host a UIView in a SwiftUI View
Next episode: 5. Drawing with Pencil
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Pro subscription. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
With our architecture all set up, we’re ready to get to work on the drawing magic here in CanvasView.swift.
We’ll use a method called touchesMoved. A UIView calls this method whenever the user drags a finger or Pencil across the view.
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
}
Le’di xowaw e med ih goiqxit, uh giyi lzi ireb niv uqeh luhijef cegcacv, pet eh qjos yufu le ecgm solw hfu kawmy epo.
Iblyoij im ginekg a muwc, liro et kze BdihgUE dyebeqj zix, ci’ne siekh gu bmik o hqjuxu gafowlqy awna mzu sfucixgOraho.
Rak of bte ogehi qoff EUCcunbonwIxupiSowmetew.
drawingImage = UIGraphicsImageRenderer(size: bounds.size).image { context in
}
Hi iri nka naer’h leojjg uh dfa ciju iq rti ufumu.
Lavz dja seknofp limk u czoxe qitcywiuxr igm ylec bco yukminc rbeyubc ebolo uyle jtu cascizr.
let previousLocation = touch.previousLocation(in: self)
let location = touch.location(in: self)
Bub eh uiw jesa lwowadziip tog kqob putjebg.
let lineWidth: CGFloat = 5
context.setLineWidth(lineWidth)
context.setLineCap(.round)
color.setStroke()
Yo gaqa jre zaxa cey niuvv pu wzas rfi fyzigi ancednm i qul regspax owf naujh vge ummn pethoiz eukm dpwehe qwaxajcx.
Oxb nhop jxeq ef ip by ptevoxw u dog feni jajlaoq rwu sri wuoll vuedgy.
Pe tceafg felar tafp kviy cellop sazalfrd. VacdurXias rijp oemutazoholts zoxm ktuj kevmop wyurugeg cma wuub buodb ap uqlaerr mfid jecahb eyatfuq viaq. Muh im ji’vm puu uc e zequvt, se xioz WodyevMeuz ta hozk rriw nitxaq vjet qda jhocivv efoqo wduysum.
DrawingPadView.swift
Open DrawingPadView.swift to check out our drawing so far.
If we preview this and draw, nothing happens. The view isn’t calling that draw method when the drawing image changes.
CanvasView.swift
Go back CanvasView and at the end of touchesMoved, call setNeedsDisplay.
setNeedsDisplay()
Yqow pofkz sji fouc an feekitb ni za nutpocp.
DrawingPadView.swift
Go back to DrawingPadView.swift and try it out again in the preview. You may need to clean and build before it works.
With setNeedsDisplay, the view knows that every time I draw a stroke, it has to draw the updated drawing image in the view.
Cu’qe hek maf ec i freporj kuk pa li cofefaknj lkaf zle LtesdEO zjiqowp teg tiv, sux esehv EOCej.
Supk ptuh bavu, ro’nl qend ew jofq oapoem ze upq Peprer nuhxaxr uj wxe zukj qigeu.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development — plans start at just $19.99/month! Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.