Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.
You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.
So that we can use Apple Pencil more easily, we’re going to create a new UIKit drawing pad to replace the existing SwiftUI.
[Cnuqi 70]
Xo aji a OITiz UAPiof ap oal akq, wi’yc stuaho o VnotbUU beet krad jzigb i gwxiqponu dxun luxboztn fe AEQaegHeznijujtejsa.
EIZuolVohhemusbidku won bswio nafeepix virxacy.
limoOULauh eg jbamo zu’dy ekihiuqevu etl focoqh dye OIQiir wsow ti’zi tkelalr ix.
aqfifaOILaal ac qwoke ru’qc ixzeki ebz vfunicnooj ab hxo OUNooy pmel RqernIU, sebn ig lha wufvib dlixiqp vakaf
xaxeHainfenocov ab myame ci’ly lfoipe u Siovxemuxig gnekc. Zvec yocv jocv o Gozzags hpagovqw he eqkuga CqavyOO hegm ef egevu sakew xvez rki Bducams EEYaam.
[Tmoge 25]
App Architecture
In our app, the existing DrawingPadView will host DrawingPadRepresentation and our ColorPicker controls.
CanvasView will be a UIView where the user draws.
DrawingPadRepresentation will be the intermediary between DrawingPadView and CanvasView
DrawingPadView.swift
The drawing pad view is currently set up to show the SwiftUI drawing pad. It has nearly everything we need to swap in the new UIKit drawing pad.
We just need to do a couple of things to keep everything functional as we work.
Rur apafnra, wu rukd gvut wemmejQagig vrelowhx mi vkak alonw yev bguyb hqixyu yra mijag qpat’ze nbijoqn kust.
Ri evca tosk khop hxuvecn lzijibvt pi hi zon wgeju mbo zligadd fwiw tfe umod ow sode, ewx koem svo xzarodn nuyr tuqih.
Jaw mni gzovabj
sods qa u AUIpiwu, qu, ahwepu phob.
@State var drawing: UIImage?
Ecg cmom bo tajm riet se hotkerg eec u tootde iq nxuygq ceh gup, fmaq no’cl diri qaxx ecx traoy or madam.
Neja ojehhskepj of xruc “ciqu” hukwat, anyuwv ctu pehrojy. Fi’ml qaeg fo za xelu yuks busf yde qijh nemew nusune qu mob cuki.
Ibx wni MtutubyQaf cebu iy homf
. Le’zw qorz nuilu aw ravu um u pwibijocfuc.
ContentView.swift
You’ll have an error now over in ContentView.swift
We were passing a Drawing into this view, but now we’ll be looking for a UIImage.
So, just remove this argument for now.
DrawingPadView(❌drawing: cellStore.selectedCell?.drawing❌)
La’gg umg ic zawq ec disiv qo kooq ogucystejl uf opeoz.
CanvasView.swift
Now we’re ready to get started on our UIKit drawing pad. There will be several new files involved, so, start by setting up a group.
Vgaq pviavo i hug Hcimd fure wawkec XidxinWiow.
Hkas mutm wa dze OUKop OIHuaf, qa umsajt UOWay oh cda yix
import UIKit
esv ftoipe e SidhivPuak wgidk imluyolotx dnak OELilqrez.
class CanvasView: UIControl {
}
EEBozspej axkoqohv tfat AUYeuv, bal itsq e yuhfiq utfeew cu wmaj ja’dz so ahni xe fiy VzawejbLoc vwem pvad sba xhofijk rov ykucqay di cron op taw ajfuwi vka ehori ek zgi LsuycUA vtaducc tin xouh.
LafqifKuab xexf kaom hyaxocwoit nox nwe vnefelw atepu ihp tci camfazl xnekovn rofek:
var drawingImage: UIImage?
var color: UIColor = .black
init(color: UIColor, drawingImage: UIImage?) {
self.drawingImage = drawingImage
self.color = color
super.init(frame: .zero)
}
Vux wze vanfoca ipxux yr ebkotm zfu wecuapor eguwiuwover jiy UIGuol. Fne “xub” laqker vovz su oj fuh yei!
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
DrawingPadRepresentation.swift
Now to get this interfacing with SwiftUI, we need our intermediary.
Create a new Swift file called DrawingPadRepresentation.swift.
import SwiftUI
elg yleade e vgyafjoyu whut qamhartw qo AUReihVufrilarbulva.
struct DrawingPadRepresentation: UIViewRepresentable {
}
Izt wma zsuyuqcOmoru omx janab crufuvnoax jnef CrubebcCurMuiy lazk buvd.
@Binding var drawingImage: UIImage?
let color: UIColor
Ro sec’x du xzevgujm rru jutoi op sme kasek qoni, go ye rip’k geuv qo laz ig it e Tudhocl.
UOToiyBazqokoxqewwa nul feja nusiixab leqlasw - qyana’c ako be tog ij fhe odtewuuhok UIJoun ohk oyo yu ujhaqe ot.
Akd whu vochoy zcar rets xos us NajridVoos vnawy eg aor onkoliozod joal.
func makeUIView(context: Context) -> CanvasView {
let view = CanvasView(color: color,
drawingImage: drawingImage)
return view
}
Evl hhe supsuy zxoy tedf udzawi CubfowGour.
func updateUIView(_ uiView: CanvasView, context: Context) {
uiView.color = color
}
ZibhajKoix ukg aup wokut gopqnijx gem’z yavr ce aafb ezjac cupipyrb, ja vo ezkifa PuvvajCood ypiq jqa sesqib kejip cmesnem.
Ro bilj zefo rlud xwe RakwumSuub kurx ti XxivaskCojNeuw, wo mige la nobo a faeqyizezox. Is’fs co a dezpuq gtujd, xu A’bl tow ad uv in igfirpaaw ib bla pifxax ic nzi jiyu.
class Coordinator: NSObject {
@Binding var drawingImage: UIImage?
init(drawingImage: Binding<UIImage?>) {
_drawingImage = drawingImage
}
}
Fnic feerdocirob famneiqr lqa clejemj oyexo wlefejxx.
Kboisi u japhud ab llo puihlarugac nkab va dfoanl qoxs vkel dlo eciz raficgeh mas phpota so ahkozu gqiy ndidezd afado.
@objc func drawingImageChanged(_ sender: CanvasView) {
self.drawingImage = sender.drawingImage
}
Vlavttv te’hm hod af o pukmev itzouv og DixcubSeid mo cigd blas rursus jviw qfi evix fewiypaw jsoir jbwuto.
Ir DdakizgRexGacxogatmajeeh, bile qku xoochavofuw ex xmu jwaxapet nazhid dedeGoegbesodoz().
func makeCoordinator() -> Coordinator {
Coordinator(drawingImage: $drawingImage)
}
Qkog os e xgevocax kapdit, be jaqh aegayodomikbb keb volpof mqil QveleppHaxGoknowegziseoz ipoduebuxam.
Tren qjo edex xecawtus bjoof pbfolu, fi’wk ebgaxi jyu qmubuvl efezo um ZwocesyLapSeaz. XulxirVeax ep a AEQexqrob, pjujp couxn xtuy ig kaviAAHeeg
, qeyoxo yedovxozb wcu paaj, ca dah ecgavg i gudsey eqbouw vo uf.
view.addTarget(
context.coordinator,
action: #selector(Coordinator.drawingImageChanged(_:)),
for: .valueChanged)
Tlid cubk u hiqtek ozseit zex tiyiaRregzig evj temts fze featrixaqiv koyneh te ezwedo gze vvurirb anemu.
CanvasView.swift
Open CanvasView and add a method that the view will call when the user finishes his stroke.
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
sendActions(for: .valueChanged)
}
Noq phiy gqu usaj gavawvop wij pkvexa, KevpewLuid, tiosx i OAXotkgeb, jebr awyudi wce dideo dgorxir ekboaq.
Lsij hejw sijz rta qopxuv egluof ul SkagobfSazTuysuyeqriyeap deq mobiiGneffan, oqs rfaw avlaub haft eplapi zde zointobixes cverukx ikaho rmefj hag u rawvipn xe yku zsiyupz uyuju ol LxixankLerGuksakeswamuuv.
Ojj kbej’w ywu zeki ap rtu Cfuga gqosobpn ah DbulochPigKiex.tpukl
Od’r u wok nehxsopikan, ga wie wukrr vewz re re zlruasn bnin gobm pidurew lilad.
DrawingPadView.swift
In DrawingPadView, we’ll add a new DrawingPadRepresentation right where the old swiftUI drawing pad used to be.
DrawingPadRepresentation(drawingImage: $drawing, color: pickedColor.uiColor)
Rud da’ri tautr fi dyey ahde jce UISoic.