OM KHODKE
OM KHODKE

Reputation: 21

Use UIPanGestureRecognizer to drag view from one position to limited position

In my application I have an UIView.I want functionality such that user can drag the view from its original position to particular limited position for this I have used **UIPanGestureRecognizer Class ** and in gestureRecognizer.state == .Changed condition I am changing the coordinates of view .I am able to drag the view to limited position when moving slowly but The problem is if the user drags the view very rapidly upward or downward the screen, then the view can be pulled beyond the limits I put on the Y position

 if(upperLimit > (self.topbaseConstrant.constant * -1))

   {
       self.topbaseConstrant.constant += gestureRecognizer.translationInView(self.view!).y

     gestureRecognizer.setTranslation(CGPointZero, inView: self.view!)

   }

I have been trying to solve the issue since last three days .Please give me suggestion Thanks in advance

Upvotes: 2

Views: 506

Answers (1)

jervine10
jervine10

Reputation: 3077

Use the min function to determine upper limits

let newPosition = topbaseConstrant.constant + panGestureRecognizer.translationInView(nil).y
topbaseConstrant.constant = min(upperLimit, newPosition)

If you drag quickly and blow past your constraint, the min function will always return that upper constraint as your new position.

Upvotes: 1

Related Questions