Skip to content

Instantly share code, notes, and snippets.

@andkon
Created June 25, 2014 18:35
Show Gist options
  • Save andkon/5eaa3bdf45baf5282908 to your computer and use it in GitHub Desktop.
Save andkon/5eaa3bdf45baf5282908 to your computer and use it in GitHub Desktop.
UIKit Gravity!
@property (strong, nonatomic) UIDynamicAnimator *animator;
@property (strong, nonatomic) UIView *boxView;
- (void)viewDidLoad
{
[super viewDidLoad];
self.boxView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.width / 2, 50, 50)];
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.boxView]];
// Determines which direction the gravity will pull the box!
gravity.gravityDirection = CGVectorMake(0,1);
[self.animator addBehavior:gravity];
// now to pair it with a collision boundary!
UICollisionBehavior *viewBoundaries = [[UICollisionBehavior alloc] initWithItems:@[self.boxView]];
viewBoundaries.translatesReferenceBoundsIntoBoundary = YES;
[self.animator addBehavior:viewBoundaries];
UIDynamicItemBehavior *dynamicBehavior = [[UIDynamicItemBehavior alloc] initWithItems:objs];
dynamicBehavior.elasticity = 0.8;
[self.animator addBehavior:dynamicBehavior];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment