Skip to content

Commit

Permalink
ADD: add BCInteractiveTransition
Browse files Browse the repository at this point in the history
  • Loading branch information
Boyce committed Jul 5, 2015
1 parent 913936b commit ce761bf
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 130 deletions.
28 changes: 28 additions & 0 deletions BCMagicTransition/BCInteractiveTransition.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// BCInteractiveTransition.h
// Demo
//
// Created by Boyce Chang on 7/6/15.
// Copyright (c) 2015 Boyce Chang. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>


typedef NS_ENUM(NSInteger, BCInteractiveTransitionType) {
BCInteractiveTransitionTypeEdgePan = 0,
BCInteractiveTransitionTypeSwipeDown,
};



@interface BCInteractiveTransition : UIPercentDrivenInteractiveTransition

+ (BCInteractiveTransition *)transitionWithType:(BCInteractiveTransitionType)type;

@property (nonatomic, assign) BOOL interacting;

- (void)addToViewController:(UIViewController*)viewController;

@end
93 changes: 93 additions & 0 deletions BCMagicTransition/BCInteractiveTransition.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// BCInteractiveTransition.m
// Demo
//
// Created by Boyce Chang on 7/6/15.
// Copyright (c) 2015 Boyce Chang. All rights reserved.
//

#import "BCInteractiveTransition.h"

@interface BCInteractiveTransition()

@property (nonatomic, assign) BCInteractiveTransitionType type;

@property (nonatomic, weak) UIViewController *presentingVC;
@property (nonatomic, strong) UIGestureRecognizer *gesture;

@end


@implementation BCInteractiveTransition

+ (BCInteractiveTransition *)transitionWithType:(BCInteractiveTransitionType)type {
BCInteractiveTransition *transition = [self new];
transition.type = type;

switch (type) {
case BCInteractiveTransitionTypeEdgePan:
{
UIScreenEdgePanGestureRecognizer *edgeGesture = [UIScreenEdgePanGestureRecognizer new];
edgeGesture.edges = UIRectEdgeLeft;
transition.gesture = edgeGesture;
}
break;
default:
break;
}

[transition.gesture addTarget:transition action:@selector(handleGestureRecognizer:)];
transition.completionSpeed = 0.999;

return transition;
}

- (void)addToViewController:(UIViewController *)viewController {
_presentingVC = viewController;
[viewController.view addGestureRecognizer:self.gesture];
}


#pragma mark - private method

- (void)handleGestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer {
CGFloat progress;

switch (self.type) {
case BCInteractiveTransitionTypeEdgePan:
progress = [gestureRecognizer translationInView:gestureRecognizer.view.superview].x / self.presentingVC.view.bounds.size.width;
break;
default:
break;
}

progress = fminf(fmaxf(progress, 0.0), 1.0);
[self handleProgress:progress];
}

- (void)handleProgress:(float)progress {
switch (self.gesture.state) {
case UIGestureRecognizerStateBegan:
self.interacting = YES;
[self.presentingVC.navigationController popViewControllerAnimated:YES];
break;
case UIGestureRecognizerStateChanged:
[self updateInteractiveTransition:progress];
break;
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled:
// Gesture over. Check if the transition should happen or not
self.interacting = NO;

if (progress <= 0.4 || self.gesture.state == UIGestureRecognizerStateCancelled) {
[self cancelInteractiveTransition];
} else {
[self finishInteractiveTransition];
}
break;
default:
break;
}
}

@end
4 changes: 2 additions & 2 deletions BCMagicTransition/BCMagicTransition.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// BCMagicTransition.h
// BCMagicTransition
//
// Created by Xaiobo Zhang on 10/21/14.
// Copyright (c) 2014 Xaiobo Zhang. All rights reserved.
// Created by Boyce Chang on 10/21/14.
// Copyright (c) 2014 Boyce Chang. All rights reserved.
//


Expand Down
70 changes: 44 additions & 26 deletions BCMagicTransition/BCMagicTransition.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,19 @@ - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionC
}

#pragma mark - private
- (UIImage *)getImageFromView:(UIView *)view {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
return image;
}


- (void)defaultAnimationFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController containerView:(UIView *)containerView duration:(NSTimeInterval)duration transitionContext:(id <UIViewControllerContextTransitioning>)transitionContext {
float deviation = (self.isPush)?1.0f:-1.0f;
float deviation = (self.isPush) ? 1.0f : -1.0f;

CGRect newFrame = toViewController.view.frame;
newFrame.origin.x += newFrame.size.width*deviation;
Expand Down Expand Up @@ -67,44 +77,52 @@ - (void)magicAnimationFromViewController:(UIViewController *)fromViewController

NSMutableArray *fromViewSnapshotArray = [[NSMutableArray alloc] init];
for (UIView *fromView in self.fromViews) {
UIView *fromViewSnapshot = [fromView snapshotViewAfterScreenUpdates:NO];
UIImageView *fromViewSnapshot = [[UIImageView alloc] initWithImage:[self getImageFromView:fromView]];
fromViewSnapshot.frame = [containerView convertRect:fromView.frame fromView:fromView.superview];
fromView.hidden = YES;
[fromViewSnapshotArray addObject:fromViewSnapshot];
fromView.alpha = 0.0;
}

toViewController.view.frame = [transitionContext finalFrameForViewController:toViewController];
toViewController.view.alpha = 0;
[containerView addSubview:toViewController.view];

for (UIView *toView in self.toViews) {
toView.hidden = YES;
toView.alpha = 0.0;
}

[containerView addSubview:toViewController.view];

for (NSUInteger i = [fromViewSnapshotArray count]; i > 0; i--) {
[containerView addSubview:[fromViewSnapshotArray objectAtIndex:i-1]];
[containerView addSubview:[fromViewSnapshotArray objectAtIndex:i - 1]];
}

[UIView animateWithDuration:duration delay:0.0 usingSpringWithDamping:0.7 initialSpringVelocity:0.8 options:UIViewAnimationOptionCurveEaseInOut animations:^{
toViewController.view.alpha = 1.0;
for (NSUInteger i = 0; i < [self.fromViews count]; i++) {
UIView *toView = [self.toViews objectAtIndex:i];
UIView *fromViewSnapshot = [fromViewSnapshotArray objectAtIndex:i];
CGRect frame = [containerView convertRect:toView.frame fromView:toView.superview];
fromViewSnapshot.frame = frame;
}
} completion:^(BOOL finished) {
for (NSUInteger i = 0; i < [self.fromViews count]; i++) {
UIView *toView = [self.toViews objectAtIndex:i];
UIView *fromView = [self.fromViews objectAtIndex:i];
UIView *fromViewSnapshot = [fromViewSnapshotArray objectAtIndex:i];
toView.hidden = NO;
fromView.hidden = NO;
[fromViewSnapshot removeFromSuperview];
}
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
[UIView animateWithDuration:duration
delay:0.0
usingSpringWithDamping:0.6
initialSpringVelocity:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
toViewController.view.alpha = 1.0;
for (NSUInteger i = 0; i < [self.fromViews count]; i++) {
UIView *toView = [self.toViews objectAtIndex:i];
UIView *fromViewSnapshot = [fromViewSnapshotArray objectAtIndex:i];
CGRect frame = [containerView convertRect:toView.frame fromView:toView.superview];
fromViewSnapshot.frame = frame;
}
} completion:^(BOOL finished) {
for (NSUInteger i = 0; i < [self.fromViews count]; i++) {
UIView *toView = [self.toViews objectAtIndex:i];
UIView *fromView = [self.fromViews objectAtIndex:i];
UIView *fromViewSnapshot = [fromViewSnapshotArray objectAtIndex:i];
toView.alpha = 1.0;
fromView.alpha = 1.0;
[fromViewSnapshot removeFromSuperview];
}

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0.1), ^{
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
});
//[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}

@end
11 changes: 8 additions & 3 deletions BCMagicTransition/UIViewController+BCMagicTransition.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@
// UIViewController+BCMagicTransition.h
// Demo
//
// Created by Nikhil Nigade on 6/4/15.
// Copyright (c) 2015 Boyce. All rights reserved.
// Created by Boyce Chang on 6/4/15.
// Copyright (c) 2015 Boyce Chang. All rights reserved.
//

#import "BCMagicTransition.h"

// Conform to this protocol on a viewController which utilizes BCMagicTransition. ViewControllers which don't will not be affected.

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@class BCMagicTransition;

@protocol BCMagicTransitionProtocol <NSObject>

@end
Expand Down
Loading

0 comments on commit ce761bf

Please sign in to comment.