Created
July 3, 2014 00:51
-
-
Save andkon/78421fef24abe6e8f851 to your computer and use it in GitHub Desktop.
Animated transitions and UIKit Dynamics
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 DismissGravity : NSObject <UIViewControllerAnimatedTransitioning> | |
@property (strong, nonatomic) UIDynamicAnimator *animator; |
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)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext; | |
{ | |
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; | |
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:[transitionContext containerView]]; | |
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[fromVC.view]]; | |
gravity.gravityDirection = CGVectorMake(0, -12); | |
[self.animator addBehavior:grabivty]; | |
gravity.action = ^{ | |
if (!CGRectIntersectsRect(fromVC.view.frame, [[transitionContext containerView] frame] | |
)) { | |
[self.animator removeAllBehaviors]; | |
[transitionContext completeTransition:YES]; | |
} | |
} | |
} | |
- (void) |
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 DropBounce: NSObject <UIViewControllerAnimatedTransitioning> | |
@property (strong, nonatomic) UIDynamicAnimator *animator; | |
@property (strong, nonatomic) id<UIViewControllerContextTransitioning> transitionContext; | |
@end |
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) animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext | |
{ | |
self.animateTransition = transitionContext; | |
UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; | |
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; | |
CGRect fromVCStartFrame = [transitionContext initialFrameForViewController:fromVC]; | |
CGRect toVCStartFrame = CGRectMake(0, | |
-CGRectGetHeight(startFrame), | |
CGRectGetWidth(startFrame), | |
CGRectGetHeight(startFrame)); | |
toVC.view.frame = toVCStartFrame; | |
[[transitionContext containerView] addSubview:toVC.view]; | |
// We use the [transitionContext containerView] for the container here. | |
// That way, the frame we refer to stays the same, regardless of what the toVC or fromVC are doing. | |
self.animator = [[UIDynamicAnimator alloc] initWithContainer:[transitionContext containerView]]; | |
UIGravityBehavior *gravity =[[UIGravityBehavior alloc] initWithItems:@[toVC.view]]; | |
gravity.gravityDirection = CGVectorMake(0, 3); | |
[self.animator addBehavior:gravity]; | |
// Now adding a boundary to just the bottom of the contianer view: | |
UICollisionBehavior *boundary = [[UICollisionBehavior alloc] initWithItems:@[toVC.view]; | |
[boundary addBoundaryWithIdentifier@"bottomEdge" | |
fromPoint:CGPointMake(0, | |
CGRectGetMaxY(fromVC.view.frame)) | |
toPoint:CGPointMake( | |
CGRectGetMaxX(fromVC.view.frame), | |
CGRectGetMaxY(fromVC.view.frame), | |
)]; | |
[self.animator addBehavior:boundary]; | |
self.animator.delegate = self; | |
} | |
- (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator | |
{ | |
[self.transitionContext completeTransition:YES]; | |
} |
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)showModal:(id)sender | |
{ | |
ModalViewController *modalVC = [[ModalViewController alloc] init]; | |
modalVC.modalPresentationStyle = UIPresentationCustom; | |
settingsVC.transitioningDelegate = self; | |
[self presentViewController:modalVC animated:YES completion:^{ | |
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)]; | |
[modalVC.hideButton addGestureRecognizer:tapGesture]; | |
}]; | |
} | |
-(id<UIViewControllerAnimatedTransitioning>) animatedViewControllerForPresentedController:(UIViewController *) presented | |
presentingController:(UIViewController *)presenting | |
sourceController:(UIViewController *)source | |
{ | |
// for presentation: | |
DropBounce *dropBounce = [[DropBounce alloc] init]; | |
return DropBounce; | |
} | |
- (id <UIViewControllerAnimatedTransitioning>) animationControllerForDismissedController:(UIViewController *)dismissed | |
{ | |
// animation for dismissal: | |
DismissGravity *dismissGravity = [[DismissGravity alloc] init]; | |
return dismissGravity; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment