ios - How to move line? -
how move horizontal line in vertical direction? can draw straight horizontal line , need move using long press. how can that?
override func touchesbegan(_ touches: set<uitouch>, event: uievent?) { if let touch = touches.first { let currentpoint = touch.location(in: self.view) drawline(frompoint: currentpoint, topoint: currentpoint) } } func drawline(frompoint: cgpoint, topoint: cgpoint) { uigraphicsbeginimagecontext(self.view.frame.size) imageview.image?.draw(in: cgrect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)) let context = uigraphicsgetcurrentcontext() let linewidth : cgfloat = 5 context?.move(to: cgpoint(x: frompoint.x, y: frompoint.y)) context?.addline(to: cgpoint(x: frompoint.x + 200, y: frompoint.y)) context?.setblendmode(cgblendmode.normal) context?.setlinecap(cglinecap.round) context?.setlinewidth(linewidth) context?.setstrokecolor(uicolor(red: 0, green: 0, blue: 0, alpha: 1.0).cgcolor) context?.strokepath() imageview.image = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() }
you can draw horizontal line using uiview , can move in vertical direction using uiview.animate method providing required options.
in viewcontroller define following variables:
var scanlinerect = cgrect.zero var scanlinestarty: cgfloat = 0 var scanlinestopy: cgfloat = 0 var topbottommargin: cgfloat = 30 var scanline: uiview = uiview()
call drawline method inside viewdidload method , call movevertically method on touch event:
func drawline() { self.addsubview(scanline) scanline.backgroundcolor = uicolor(red: 0.4, green: 0.8, blue: 0.4, alpha: 1.0) // green color scanlinerect = cgrect(x: 0, y: 0, width: self.frame.width, height: 2) scanlinestarty = topbottommargin scanlinestopy = self.frame.size.height - topbottommargin } func movevertically() { scanline.frame = scanlinerect scanline.center = cgpoint(x: scanline.center.x, y: scanlinestarty) scanline.ishidden = false weak var weakself = scanline uiview.animate(withduration: 1.0, delay: 0.0, options: [.repeat, .autoreverse, .beginfromcurrentstate], animations: {() -> void in weakself!.center = cgpoint(x: weakself!.center.x, y: scanlinestopy) }, completion: nil) }
Comments
Post a Comment