swift - Drawing a line on view/imageview in iOS/swift3 -
i found question similar own here: draw line realtime swift 3.0
after piecing have in own program, not sure make call drawlinefrom(frompoint: cgpoint, topoint: cgpoint) in code can draw. app runs, still can't draw. code below.
override func touchesbegan(_ touches: set<uitouch>, event: uievent?) { swiped = false if let touch = touches.first { lastpoint = touch.location(in: self.view) } } func drawlinefrom(frompoint: cgpoint, topoint: cgpoint) { uigraphicsbeginimagecontextwithoptions(view.bounds.size, false, 0) tempimageview.image?.draw(in: view.bounds) let context = uigraphicsgetcurrentcontext() context?.move(to: frompoint) context?.addline(to: topoint) context?.setlinecap(cglinecap.round) context?.setlinewidth(brushwidth) context?.setstrokecolor(red: red, green: green, blue: blue, alpha: 1.0) context?.setblendmode(cgblendmode.normal) context?.strokepath() tempimageview.image = uigraphicsgetimagefromcurrentimagecontext() tempimageview.alpha = opacity uigraphicsendimagecontext() } override func touchesmoved(_ touches: set<uitouch>, event: uievent?) { swiped = true if let touch = touches.first { let currentpoint = touch.location(in: view) drawlinefrom(frompoint: lastpoint, topoint: currentpoint) lastpoint = currentpoint } } override func touchesended(_ touches: set<uitouch>, event: uievent?) { if !swiped { // draw single point self.drawlinefrom(frompoint: lastpoint, topoint: lastpoint) } } let tempimageview = uiimageview() override func viewdidload() { super.viewdidload() view.backgroundcolor = uicolor(r: 21, g: 221, b: 125) //view.addsubview(titlelabel) //view.addsubview(learnbutton) //view.addsubview(drawbutton) //view.addsubview(mainimageview) view.addsubview(tempimageview) //setuptitlelabel() //setuplearnbutton() //setupdrawbutton() setupimageviews() self.navigationcontroller?.isnavigationbarhidden = true } func setupimageviews() { tempimageview.widthanchor.constraint(equalto: view.widthanchor) tempimageview.heightanchor.constraint(equalto: view.heightanchor) }
Comments
Post a Comment