Created
April 28, 2014 14:06
-
-
Save chourobin/11373167 to your computer and use it in GitHub Desktop.
Example using pan gestures and push collision
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UIView *view = [panGesture view]; | |
if (panGesture.state == UIGestureRecognizerStateBegan) { | |
self.collectionView.panGestureRecognizer.enabled = NO; | |
self.offsetY = view.frame.origin.y; | |
} else if (panGesture.state == UIGestureRecognizerStateBegan || panGesture.state == UIGestureRecognizerStateChanged) | |
{ | |
CGPoint translation = [panGesture translationInView:[view superview]]; | |
if (view.frame.origin.y <= 0 && translation.y < 0) { | |
return; | |
} | |
[view setCenter:CGPointMake([view center].x, [view center].y + translation.y)]; | |
[panGesture setTranslation:CGPointZero inView:view]; | |
} else if (panGesture.state == UIGestureRecognizerStateEnded) { | |
CGPoint velocity = [panGesture velocityInView:view]; | |
UIPushBehavior *pushBehavior = [[UIPushBehavior alloc] init]; | |
UICollisionBehavior *collision = [[UICollisionBehavior alloc] init]; | |
collision.collisionDelegate = self; | |
[collision addItem:self.collectionView]; | |
if (self.offsetY < view.frame.origin.y) { | |
pushBehavior.pushDirection = CGVectorMake(0, 1.0); | |
[collision addBoundaryWithIdentifier:@"max" fromPoint:CGPointMake(0, 2*self.view.frame.size.height + 1) toPoint:CGPointMake(self.view.frame.size.width, 2*self.view.frame.size.height + 1)]; | |
} else { | |
pushBehavior.pushDirection = CGVectorMake(0, -1.0); | |
[collision addBoundaryWithIdentifier:@"min" fromPoint:CGPointMake(0, 0) toPoint:CGPointMake(self.view.frame.size.width, 0)]; | |
} | |
[pushBehavior addItem:self.collectionView]; | |
pushBehavior.magnitude = fabs(velocity.y); | |
[self.animator addBehavior:pushBehavior]; | |
[self.animator addBehavior:collision]; | |
[self.animator updateItemUsingCurrentState:self.collectionView]; | |
self.collectionView.panGestureRecognizer.enabled = YES; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment