ios - Creating App to create circles when users taps view -
i ask if figure out what's wrong code, can't seem create circle when there touch event.
override func viewdidload() { super.viewdidload() self.view.backgroundcolor = uicolor.lightgray // additional setup after loading view, typically nib. } func touchesbegan(touches: set<nsobject>, withevent event: uievent) { if let touch = touches as? set<uitouch> { let circlecenter = touch.first!.location(in: view) let circlewidth = cgfloat(25) let circleheight = circlewidth let circleview = circleview(frame: cgrect(x: circlecenter.x, y: circlecenter.y, width: circlewidth, height: circleheight)) view.addsubview(circleview) } } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } if need code subview circleview:
class circleview: uiview { override init(frame: cgrect) { super.init(frame: frame) self.backgroundcolor = uicolor.clear } required init(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } func draw(rect: cgrect) { var context = uigraphicsgetcurrentcontext(); context!.setlinewidth(5.0) uicolor.red.set() let center = cgpoint(x: round(frame.size.width)/(2), y: round(frame.size.height)/2) let radius = (round(frame.size.width) - 10)/(2) context!.addarc(center:center, radius:radius, startangle:0, endangle:cgfloat(double.pi) * 2, clockwise:true) context!.strokepath(); }
two of functions incorrect (as of in swift 3). change
func touchesbegan(touches: set<nsobject>, withevent event: uievent) to
override func touchesbegan(_ touches: set<uitouch>, event: uievent?) and change
func draw(rect: cgrect) to
override func draw(_ rect: cgrect)
Comments
Post a Comment