3

I've got a view within a navigation controller.

I am then adding a subview to this view and offsetting its origin height so that it covers only half the screen (with the other half overflowing off out the bottom of the screen). I want to be able to then drag this view upwards but it get stopped when it hits the bottom of the navigation bar.

I'm using a UIPanGestureRecognizer to handle the dragging of the view and this works fine but it just won't seem to stop at the boundary.

This is the code I'm using:

bottomNavbarY = UIApplication.sharedApplication().statusBarFrame.size.height + self.navigationController!.navigationBar.frame.size.height

view.addSubview(pullover.view)
pullover.view.frame.origin.y = pulloverOffset

var animator = UIDynamicAnimator(referenceView: view)
var collision = UICollisionBehavior(items: [pullover.view])
collision.translatesReferenceBoundsIntoBoundary = true
collision.addBoundaryWithIdentifier("upper", fromPoint: CGPointMake(0, bottomNavbarY), toPoint: CGPointMake(UIScreen.mainScreen().bounds.size.width, bottomNavbarY))
animator.addBehavior(collision)

However, when I drag the covering view up it never interacts with any boundary, it just passes straight through. What am I doing wrong? Is it possible to use boundaries to stop views that are being dragged by the user in this way?

myles
  • 1,681
  • 1
  • 15
  • 27

1 Answers1

6

when I drag the covering view up it never interacts with any boundary

You've misunderstood this feature. Collision boundaries are for when UIKit Dynamics is in charge of moving views around. Dragging is when the user is in charge of moving views around. If you want the drag to stop at a certain point, it's up to you to in the gesture recognizer handler to think about where the view is and not move the view if you don't want it to move.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Damn. Thought this might be the case. I tried doing it in the gesture recogniser handler earlier but was having issues with stopping it from passing a point if it was being dragged very quickly. Do you know of a way to overcome this? – myles Jul 09 '15 at 19:38
  • I don't know what "overcome this" means. If you have a question about how to make an object being dragged with a pan gesture recognizer stop before passing some imaginary boundary, ask that as a new question. I know how _I_ do it! But I believe I've answered the question you actually asked. – matt Jul 09 '15 at 19:44
  • Lol, seems like you know exactly what I meant by 'overcome this'... Thanks for the help anyway. – myles Jul 09 '15 at 19:51