Last active
August 29, 2015 14:20
-
-
Save kmdarshan/4fa27d7429412833e9f1 to your computer and use it in GitHub Desktop.
UIDynamicKit : Most commonly used animation behaviors
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
@interface RRHomeViewController () | |
{ | |
UIButton *logoutButton; | |
UIView *testAnimateView; | |
} | |
@property (nonatomic, strong) UIDynamicAnimator *animator; | |
@property (nonatomic, strong) UIDynamicItemBehavior *linearVelocity; | |
@end | |
@implementation RRHomeViewController | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
logoutButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 50 , 200, 50)]; | |
[logoutButton addTarget:self action:@selector(logoutFromFacebook) forControlEvents:UIControlEventTouchUpInside]; | |
[logoutButton setTitle:@"Logout from facebook" forState:UIControlStateNormal]; | |
[logoutButton setBackgroundColor:[UIColor blackColor]]; | |
[logoutButton.titleLabel setTextAlignment:NSTextAlignmentCenter]; | |
[self.view addSubview:logoutButton]; | |
testAnimateView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height , 200, 50)]; | |
[testAnimateView setBackgroundColor:[UIColor blackColor]]; | |
[self.view addSubview:testAnimateView]; | |
// initalize the animator over here | |
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; | |
} | |
-(void) viewDidAppear:(BOOL)animated { | |
[super viewDidAppear:YES]; | |
// [self pushUp]; | |
// [self testSnapBehavior]; | |
// [self pushDown]; | |
} | |
// this will bring the button down | |
-(void) pushDown { | |
[self.animator removeAllBehaviors]; | |
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[logoutButton]]; | |
[gravityBehavior setMagnitude:3.0f]; // increase the speed | |
[self.animator addBehavior:gravityBehavior]; | |
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[logoutButton]]; | |
// add the boundary here | |
[collisionBehavior addBoundaryWithIdentifier:@"bottomCollisionBoundary" | |
fromPoint:CGPointMake(0.0f, self.view.bounds.size.height - 100.0f) | |
toPoint:CGPointMake(self.view.bounds.size.width,self.view.bounds.size.height - 100.0f)]; | |
[self.animator addBehavior:collisionBehavior]; | |
// uncomment this if you want to set the bounds to the actual view | |
// and not stop at your boundary | |
// collisionBehavior.translatesReferenceBoundsIntoBoundary = YES; | |
UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[logoutButton]]; | |
itemBehavior.elasticity = 0.4; // number of bounces | |
[self.animator addBehavior:itemBehavior]; | |
} | |
-(void) testSnapBehavior { | |
[self.animator removeAllBehaviors]; | |
UISnapBehavior *snapBehavior = [[UISnapBehavior alloc] initWithItem:logoutButton snapToPoint:CGPointMake(100.0f, 300.0f)]; | |
snapBehavior.damping = 0.8; | |
[self.animator addBehavior:snapBehavior]; | |
} | |
-(void) pushUp { | |
[self.animator removeAllBehaviors]; // remove all behaviors for this | |
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; | |
UIPushBehavior *pushBehavior = [[UIPushBehavior alloc] initWithItems:@[testAnimateView] mode:UIPushBehaviorModeInstantaneous]; | |
[pushBehavior setAngle:M_PI_2 magnitude:1.0]; | |
[self.animator addBehavior:pushBehavior]; | |
UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[testAnimateView]]; | |
// this should change the direction of the gravity | |
[gravityBehavior setGravityDirection:CGVectorMake(0.0, -1.0)]; | |
[self.animator addBehavior:gravityBehavior]; | |
UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[testAnimateView]]; | |
[collisionBehavior addBoundaryWithIdentifier:@"alertCollisionBoundary" | |
fromPoint:CGPointMake(testAnimateView.frame.origin.x, self.view.frame.origin.y + 200.0) | |
toPoint:CGPointMake(testAnimateView.frame.origin.y, self.view.frame.origin.y + 200.0)]; | |
[self.animator addBehavior:collisionBehavior]; | |
UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[testAnimateView]]; | |
itemBehavior.elasticity = 0.4; // number of bounces | |
[self.animator addBehavior:itemBehavior]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment