summaryrefslogtreecommitdiff
path: root/SmartDeviceLink-iOS/SmartDeviceLink_Example/Classes/ConnectionTransitionContext.m
blob: 4521cd5b19486af694c0240e1696d27cb10e24b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
//  ConnectionTransitionContext.m
//  SmartDeviceLink-iOS
//
//  Created by Joel Fischer on 2/18/15.
//  Copyright (c) 2015 smartdevicelink. All rights reserved.
//

#import "ConnectionTransitionContext.h"



@interface ConnectionTransitionContext ()

@property (assign, nonatomic) UIModalTransitionStyle presentationStyle;

@property (weak, nonatomic) UIView *containerView;
@property (strong, nonatomic) NSDictionary *viewControllers;

@property (assign, nonatomic) CGRect disappearingFromRect;
@property (assign, nonatomic) CGRect disappearingToRect;
@property (assign, nonatomic) CGRect appearingFromRect;
@property (assign, nonatomic) CGRect appearingToRect;

@property (copy, nonatomic) TransitionCompleteBlock block;

@end



@implementation ConnectionTransitionContext

- (instancetype)initWithFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController direction:(ConnectionTransitionDirection)direction transitionComplete:(TransitionCompleteBlock)completion {
    NSAssert((fromViewController.isViewLoaded && fromViewController.view.superview), @"The fromViewController must reside in the container view when initializing the transition context");
    
    self = [super init];
    if (!self) {
        return nil;
    }
    
    self.presentationStyle = UIModalPresentationCustom;
    self.containerView = fromViewController.view.superview;
    self.viewControllers = @{
                             UITransitionContextFromViewControllerKey: fromViewController,
                             UITransitionContextToViewControllerKey: toViewController
                             };
    
    CGFloat travelDistance = (direction == ConnectionTransitionDirectionRight) ? -self.containerView.bounds.size.width : self.containerView.bounds.size.width;
    self.disappearingFromRect = self.appearingToRect = self.containerView.bounds;
    self.disappearingToRect = CGRectOffset(self.containerView.bounds, travelDistance, 0);
    self.appearingFromRect = CGRectOffset(self.containerView.bounds, -travelDistance, 0);
    
    self.block = completion;
    
    return self;
}

#pragma mark - UIViewControllerContextTransitioning

- (CGRect)initialFrameForViewController:(UIViewController *)vc {
    if (vc == [self viewControllerForKey:UITransitionContextFromViewControllerKey]) {
        return self.disappearingFromRect;
    } else {
        return self.appearingFromRect;
    }
}

- (CGRect)finalFrameForViewController:(UIViewController *)vc {
    if (vc == [self viewControllerForKey:UITransitionContextFromViewControllerKey]) {
        return self.disappearingToRect;
    } else {
        return self.appearingToRect;
    }
}

- (UIViewController *)viewControllerForKey:(NSString *)key {
    return self.viewControllers[key];
}

- (void)completeTransition:(BOOL)didComplete {
    if (self.block != NULL) {
        self.block(didComplete);
    }
}

- (BOOL)transitionWasCancelled {
    return NO; // TODO: If made interactive, it can be cancelled
}

- (UIView *)viewForKey:(NSString *)key {
    UIViewController *viewController = nil;
    if ([key isEqualToString:UITransitionContextFromViewControllerKey]) {
        viewController = [self viewControllerForKey:UITransitionContextFromViewControllerKey];
    } else {
        viewController = [self viewControllerForKey:UITransitionContextToViewControllerKey];
    }
    
    return viewController.view;
}

- (CGAffineTransform)targetTransform {
    return CGAffineTransformIdentity;
}


#pragma mark Interactive Transition

- (void)updateInteractiveTransition:(CGFloat)percentComplete {}
- (void)finishInteractiveTransition {}
- (void)cancelInteractiveTransition {}


@end