ios - Stop UIView from going off screen -


i have uiview circle. app - can move circle around screen using uipangesturerecognizer. don't want circle able dragged off screen. example, if drag circle right, should stop moving circle when right edge hits edge of window.

here code:

 switch rec.state {         case .began:             x = fingerlocation.x - (myview?.center.x)!             y = fingerlocation.y - (myview?.center.y)!             break           case .changed:             myview?.center.x = fingerlocation.x - x             myview?.center.y = fingerlocation.y - y              if (myview?.center.x)! + (myview!.bounds.width/2) >= view.bounds.width {                 myview?.center.x = view.bounds.width - myview!.bounds.width/2             }             break         case .ended:             myview?.center = cgpointmake(fingerlocation.x - x, fingerlocation.y - y)             break } 

this code works if drag circle toward edge. if drag quickly, circle go on edge, , jump view second .changed state sent. how can stop circle ever going on edge?

you check first if fingerlocation result in offscreen view,
, move view if not move offscreen.

case .changed:     let currentrightedge = cgrectgetmaxx(myview!.frame)    let potentialrightedge = currentrightedge + fingerlocation.x - x    if  potentialrightedge >= view.bounds.width {      myview?.center.x = view.bounds.width - myview!.bounds.width/2    }    else {      myview?.center.x = potentialrightedge    }    myview?.center.y = fingerlocation.y - y 

also, not think need break in swift ;-).


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -