I managed to stop my UIView going beyond the container using pan gestures, but I try my best to stop it around the edges. Currently, it goes straight to the last edge of the pixel, and not to the edge of the moved view.
I tried to change the value of the frame, which, in my opinion, is a way out, but cannot lead to the correct results. Not sure how close I have or have an easier way?
How would I stop it at the edges of the moved view instead of the last pixel? 
//Container is self.sliderContainer //Green Block is sender.view which has a UIGesture applied var location = sender.locationInView(self.view) var newFrame = CGRectMake(0, 0, self.sliderContainer.frame.width, self.sliderContainer.frame.height) let obstacleViewFrame = self.view.convertRect(self.sliderContainer.frame, fromView: self.sliderContainer.superview) // Check if the touch is inside the obstacle view if CGRectContainsPoint(obstacleViewFrame, location) { sender.view!.center.x = sender.view!.center.x + translation.x sender.setTranslation(CGPointZero, inView: self.view) }
source share