ios - let user to draw rectangle to select an area -


i'm new in swift , i'm trying let user draw rectangle (touching , dragging) select area of image when cropping don't want crop want know cgrect user created.

so far have .xib uiimage inside , viewcontroller. want draw above image every tutorial found drawing subclassing uiview, override drawrect , put xib class.

i figured out. created uiview , change frame depending on touches events

let overlay = uiview() var lastpoint = cgpointzero  override func viewdidload() {     super.viewdidload()      // additional setup after loading view.     overlay.layer.bordercolor = uicolor.blackcolor().cgcolor     overlay.backgroundcolor = uicolor.clearcolor().colorwithalphacomponent(0.5)     overlay.hidden = true     self.view.addsubview(overlay)  }  override func touchesbegan(touches: set<uitouch>, withevent event: uievent?) {      //save original tap point     if let touch = touches.first {         lastpoint = touch.locationinview(self.view)     }    }  override func touchesmoved(touches: set<uitouch>, withevent event: uievent?) {     //get current known point , redraw     if let touch = touches.first {         let currentpoint = touch.locationinview(view)         redrawselectionarea(lastpoint, topoint: currentpoint)     } }  func redrawselectionarea(frompoint: cgpoint, topoint: cgpoint) {     overlay.hidden = false          //calculate rect original point , last known point         let rect = cgrectmake(min(frompoint.x, topoint.x),         min(frompoint.y, topoint.y),         fabs(frompoint.x - topoint.x),         fabs(frompoint.y - topoint.y));      overlay.frame = rect }  override func touchesended(touches: set<uitouch>, withevent event: uievent?) {     overlay.hidden = true      //user has lift finger, use rect     applyfiltertoselectedarea(overlay.frame)      overlay.frame = cgrectzero //reset overlay next tap } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -