Search code examples
iosswiftboundaryuidynamicanimatoruicollisionbehavior

UICollisionBehavior boundary not working


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?


Solution

  • 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.