Created
June 25, 2014 18:35
-
-
Save andkon/5eaa3bdf45baf5282908 to your computer and use it in GitHub Desktop.
UIKit Gravity!
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
@property (strong, nonatomic) UIDynamicAnimator *animator; | |
@property (strong, nonatomic) UIView *boxView; |
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
- (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