summaryrefslogtreecommitdiff
path: root/SmartDeviceLink_Example/Classes/ConnectionContainerViewController.m
blob: 544a9f3c91e7010d69801d5222f736c63ca13354 (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
114
115
116
117
118
119
120
121
//
//  ConnectionContainerViewController.m
//  SmartDeviceLink-iOS

#import "ConnectionContainerViewController.h"

#import "ConnectionTCPTableViewController.h"
#import "ConnectionIAPTableViewController.h"
#import "ConnectionTransitionContext.h"
#import "ConnectionAnimatedTransition.h"



@interface ConnectionContainerViewController ()

@property (weak, nonatomic) IBOutlet UISegmentedControl *connectionTypeSegmentedControl;
@property (strong, nonatomic) NSArray<UIViewController *> *viewControllers;
@property (strong, nonatomic) UIViewController *currentViewController;

@property (strong, nonatomic) UIPanGestureRecognizer *panGestureRecognizer;

@end



@implementation ConnectionContainerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    
    // Setup the child VCs
    UIStoryboard *tcpControllerStoryboard = [UIStoryboard storyboardWithName:@"ConnectionTCPTableViewController" bundle:[NSBundle mainBundle]];
    UIStoryboard *iapControllerStoryboard = [UIStoryboard storyboardWithName:@"ConnectionIAPTableViewController" bundle:[NSBundle mainBundle]];
    ConnectionTCPTableViewController *tcpController = [tcpControllerStoryboard instantiateInitialViewController];
    ConnectionIAPTableViewController *iapController = [iapControllerStoryboard instantiateInitialViewController];
    self.viewControllers = @[tcpController, iapController];
    
    // Setup the pan gesture
    self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizerDidFire:)];
    [self.view addGestureRecognizer:self.panGestureRecognizer];
    
    // Setup initial view controller state
    self.connectionTypeSegmentedControl.selectedSegmentIndex = 0;
    [self loadInitialChildViewController];
}

- (void)loadInitialChildViewController {
    // On the initial load, we just add the new child VC with no animation
    UIViewController *initialViewController = self.viewControllers[0];
    [self addChildViewController:initialViewController];
    [self.view addSubview:initialViewController.view];
    [initialViewController didMoveToParentViewController:self];
    
    self.currentViewController = initialViewController;
}


#pragma mark - IBActions

- (IBAction)connectionTypeSegmentedControlSelectedIndexDidChange:(UISegmentedControl *)sender {
    [self transitionToViewControllerForSelectedIndex:sender.selectedSegmentIndex];
}


#pragma mark - Gestures

- (void)panGestureRecognizerDidFire:(UIPanGestureRecognizer *)gesture {
    BOOL goingRight = ([gesture velocityInView:gesture.view].x < 0.0f);
    
    NSUInteger currentSegmentIndex = self.connectionTypeSegmentedControl.selectedSegmentIndex;
    if (goingRight && (currentSegmentIndex != self.viewControllers.count - 1)) {
        // If we're swiping left (going right) and current segment is not all the way to the right
        NSUInteger nextIndex = currentSegmentIndex + 1;
        self.connectionTypeSegmentedControl.selectedSegmentIndex = nextIndex;
        [self transitionToViewControllerForSelectedIndex:nextIndex];
    } else if (!goingRight && (currentSegmentIndex > 0)) {
        // If we're swiping right (going left) and the current segment is not all the way to the left
        NSUInteger nextIndex = currentSegmentIndex - 1;
        self.connectionTypeSegmentedControl.selectedSegmentIndex = nextIndex;
        [self transitionToViewControllerForSelectedIndex:nextIndex];
    }
}


#pragma mark - Private API

- (void)transitionToViewControllerForSelectedIndex:(NSInteger)selectedIndex {
    UIViewController *toViewController = self.viewControllers[selectedIndex];
    
    if (toViewController == nil || toViewController == self.currentViewController) {
        return;
    }
    
    [self.currentViewController willMoveToParentViewController:nil];
    [self addChildViewController:toViewController];

    id<UIViewControllerAnimatedTransitioning> animator = [[ConnectionAnimatedTransition alloc] init];
    NSUInteger fromIndex = [self.viewControllers indexOfObject:self.currentViewController];
    
    ConnectionTransitionContext *transitionContext = [[ConnectionTransitionContext alloc] initWithFromViewController:self.currentViewController toViewController:toViewController direction:((selectedIndex > fromIndex) ? ConnectionTransitionDirectionRight : ConnectionTransitionDirectionLeft) transitionComplete:^(BOOL didComplete) {
        [self.currentViewController.view removeFromSuperview];
        [self.currentViewController removeFromParentViewController];
        [toViewController didMoveToParentViewController:self];
        
        if ([animator respondsToSelector:@selector(animationEnded:)]) {
            [animator animationEnded:didComplete];
        }
        
        self.connectionTypeSegmentedControl.userInteractionEnabled = YES;
        self.currentViewController = toViewController;
    }];
    transitionContext.animated = YES;
    transitionContext.interactive = NO;
    
    self.connectionTypeSegmentedControl.userInteractionEnabled = NO;
    [animator animateTransition:transitionContext];
}

@end