I was looking for the same solution myself, but setting allowsRotation
on a UIDynamicItemBehavior
didn't quite give me the effect I was looking for.
I was able to prevent any movement from UISnapBehavior
by placing using UICollisionBehavior
and placing two boundaries on either side of the view I didn't want to move (in my case, I configured boundaries on left and right side of the view to prevent x-axis movement). To get a nice bouncy effect I also set friction
to 0 (using UIDynamicItemBehavior
) and adjusted the damping
on UISnapBehavior
to get the right amount of bounce.
Here's an example of what worked for me:
[self.animator removeAllBehaviors];
UISnapBehavior *snapBackBehavior = [[UISnapBehavior alloc] initWithItem:self.snappingView snapToPoint:self.slidePanelCenter];
snapBackBehavior.damping = 0.2;
[self.animator addBehavior:snapBackBehavior];
UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.snappingView]];
itemBehavior.friction = 0.0;
[self.animator addBehavior:itemBehavior];
CGPoint leftBoundaryStart = CGPointMake(self.snappingView.frame.origin.x, 0.0);
CGPoint leftBoundaryEnd = CGPointMake(leftBoundaryStart.x, self.view.bounds.size.height);
CGPoint rightBoundaryStart = CGPointMake(leftBoundaryStart.x + self.snappingView.frame.size.width, 0.0);
CGPoint rightBoundaryEnd = CGPointMake(rightBoundaryStart.x, leftBoundaryEnd.y);
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.snappingView]];
[collisionBehavior addBoundaryWithIdentifier:@"leftEdge" fromPoint:leftBoundaryStart toPoint:leftBoundaryEnd];
[collisionBehavior addBoundaryWithIdentifier:@"rightEdge" fromPoint:rightBoundaryStart toPoint:rightBoundaryEnd];
[self.animator addBehavior:collisionBehavior];