|
| 1 | +// |
| 2 | +// CRPageViewController.m |
| 3 | +// Pods |
| 4 | +// |
| 5 | +// Created by Sergey Chuchukalo on 5/25/16. |
| 6 | +// |
| 7 | +// Copyright © 2016 Cleveroad Inc. All rights reserved. |
| 8 | + |
| 9 | +#import "CRPageViewController.h" |
| 10 | + |
| 11 | +@interface CRPageViewController () <UIGestureRecognizerDelegate> |
| 12 | + |
| 13 | +- (void)slideToRightWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer; |
| 14 | +- (void)slideToLeftWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer; |
| 15 | +@property (strong, nonatomic) NSMutableArray <UIViewController*> * internalViewControllers; |
| 16 | +@property (assign, nonatomic) CGFloat spaceHorizontal; |
| 17 | +@property (assign, nonatomic) CGFloat spaceVertical; |
| 18 | + |
| 19 | +@end |
| 20 | + |
| 21 | +@implementation CRPageViewController |
| 22 | + |
| 23 | +#pragma mark - Life circle VC |
| 24 | + |
| 25 | +- (void)viewDidLoad { |
| 26 | + [super viewDidLoad]; |
| 27 | + |
| 28 | + UISwipeGestureRecognizer *swipeRightOrange = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideToRightWithGestureRecognizer:)]; |
| 29 | + swipeRightOrange.direction = UISwipeGestureRecognizerDirectionRight; |
| 30 | + [self.view addGestureRecognizer:swipeRightOrange]; |
| 31 | + UISwipeGestureRecognizer *swipeLeftOrange = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(slideToLeftWithGestureRecognizer:)]; |
| 32 | + swipeLeftOrange.direction = UISwipeGestureRecognizerDirectionLeft; |
| 33 | + [self.view addGestureRecognizer:swipeLeftOrange]; |
| 34 | +} |
| 35 | + |
| 36 | +- (void)viewDidAppear:(BOOL)animated { |
| 37 | + [super viewDidAppear:animated]; |
| 38 | + |
| 39 | + self.spaceHorizontal = (self.view.frame.size.width - self.childVCSize.width) / 2; |
| 40 | + self.spaceVertical = (self.view.frame.size.height - self.childVCSize.height) / 2; |
| 41 | + self.internalViewControllers = [NSMutableArray new]; |
| 42 | + for (UIViewController* vc in self.viewControllers) { |
| 43 | + [self pushRightViewController:vc]; |
| 44 | + } |
| 45 | + [self changeViewControllersWithNumber:self.countPageInController/2]; |
| 46 | + [self performSelector:@selector(focusedCentralView) withObject:nil afterDelay:0.2]; |
| 47 | +} |
| 48 | + |
| 49 | +- (void)focusedCentralView { |
| 50 | + if ([self.dataSource respondsToSelector:@selector(focusedViewController:)]) { |
| 51 | + [self.dataSource focusedViewController:self.internalViewControllers[self.countPageInController/2]]; |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +- (void)setSizeBetweenVC:(CGFloat)sizeBetweenVC { |
| 56 | + _sizeBetweenVC = sizeBetweenVC; |
| 57 | + [self changeViewControllersWithNumber:self.countPageInController/2]; |
| 58 | +} |
| 59 | + |
| 60 | +- (void)setChildVCSize:(CGSize)childVCSize { |
| 61 | + _childVCSize = childVCSize; |
| 62 | + self.spaceHorizontal = (self.view.frame.size.width - self.childVCSize.width) / 2; |
| 63 | + self.spaceVertical = (self.view.frame.size.height - self.childVCSize.height) / 2; |
| 64 | + [self changeViewControllersWithNumber:self.countPageInController/2]; |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +- (void)setOffsetOfHeightCentralVC:(CGFloat)OffsetOfHeightCentralVC { |
| 69 | + _OffsetOfHeightCentralVC = OffsetOfHeightCentralVC; |
| 70 | + [self changeViewControllersWithNumber:self.countPageInController/2]; |
| 71 | +} |
| 72 | + |
| 73 | +#pragma mark - Push VC |
| 74 | + |
| 75 | +- (void)pushRightViewController:(__kindof UIViewController *)viewController { |
| 76 | + viewController.view.frame = CGRectMake(self.view.frame.size.width, |
| 77 | + self.spaceVertical, |
| 78 | + self.childVCSize.width, |
| 79 | + self.childVCSize.height); |
| 80 | + [self createViewController:viewController]; |
| 81 | + [self.internalViewControllers addObject:viewController]; |
| 82 | + [self changeViewControllersWithNumber:self.countPageInController/2]; |
| 83 | +} |
| 84 | + |
| 85 | +- (void)pushLeftViewController:(__kindof UIViewController *)viewController { |
| 86 | + viewController.view.frame = CGRectMake(0, |
| 87 | + self.spaceVertical, |
| 88 | + self.childVCSize.width, |
| 89 | + self.childVCSize.height); |
| 90 | + [self createViewController:viewController]; |
| 91 | + [self.internalViewControllers insertObject:viewController atIndex:0]; |
| 92 | + [self changeViewControllersWithNumber:self.countPageInController/2 + 1]; |
| 93 | +} |
| 94 | + |
| 95 | +# pragma mark - gestureRecognizers |
| 96 | + |
| 97 | +- (void)slideToRightWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer{ |
| 98 | + if ([self.dataSource respondsToSelector:@selector(pageViewController:viewControllerBeforeViewController:)]) { |
| 99 | + UIViewController *newViewController = [self.dataSource pageViewController:self viewControllerBeforeViewController:self.internalViewControllers.firstObject]; |
| 100 | + if (newViewController) { |
| 101 | + [self unfocused:self.countPageInController/2]; |
| 102 | + [self pushLeftViewController:newViewController]; |
| 103 | + [UIView animateKeyframesWithDuration:self.animationSpeed delay:0 options:self.animation animations:^{ |
| 104 | + [self changeViewControllersWithNumber:self.countPageInController/2]; |
| 105 | + if ([self.dataSource respondsToSelector:@selector(focusedViewController:)]) { |
| 106 | + [self.dataSource focusedViewController:self.internalViewControllers[self.countPageInController/2]]; |
| 107 | + } |
| 108 | + } completion:^(BOOL finished) { |
| 109 | + }]; |
| 110 | + [self performSelector:@selector(popRightViewCintroller) withObject:nil afterDelay:self.animationSpeed]; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +- (void)slideToLeftWithGestureRecognizer:(UISwipeGestureRecognizer *)gestureRecognizer { |
| 116 | + if ([self.dataSource respondsToSelector:@selector(pageViewController:viewControllerAfterViewController:)]) { |
| 117 | + UIViewController *newViewController = [self.dataSource pageViewController:self viewControllerAfterViewController:self.internalViewControllers.lastObject]; |
| 118 | + if (newViewController) { |
| 119 | + [self unfocused:self.countPageInController/2]; |
| 120 | + [self pushRightViewController:newViewController]; |
| 121 | + [UIView animateKeyframesWithDuration:self.animationSpeed delay:0 options:self.animation animations:^{ |
| 122 | + [self changeViewControllersWithNumber:self.countPageInController/2 + 1]; |
| 123 | + if ([self.dataSource respondsToSelector:@selector(focusedViewController:)]) { |
| 124 | + [self.dataSource focusedViewController:self.internalViewControllers[self.countPageInController/2 + 1]]; |
| 125 | + } |
| 126 | + } completion:^(BOOL finished) { |
| 127 | + }]; |
| 128 | + [self performSelector:@selector(popLeftViewCintroller) withObject:nil afterDelay:self.animationSpeed]; |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +#pragma mark - Helpers |
| 134 | + |
| 135 | +- (void)popRightViewCintroller { |
| 136 | + if (self.internalViewControllers.count >= 0) { |
| 137 | + [self removeViewController:self.internalViewControllers.lastObject]; |
| 138 | + [self.internalViewControllers removeLastObject]; |
| 139 | + [self changeViewControllersWithNumber:self.countPageInController/2]; |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +- (void)popLeftViewCintroller { |
| 144 | + if (self.internalViewControllers.count >= 0) { |
| 145 | + [self removeViewController:self.internalViewControllers.firstObject]; |
| 146 | + [self.internalViewControllers removeObjectAtIndex:0]; |
| 147 | + [self changeViewControllersWithNumber:self.countPageInController/2]; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +- (void)createViewController:(__kindof UIViewController *)viewController { |
| 152 | + [self addChildViewController:viewController]; |
| 153 | + [self.view addSubview:viewController.view]; |
| 154 | + [viewController didMoveToParentViewController:self]; |
| 155 | +} |
| 156 | + |
| 157 | +- (void)removeViewController:(__kindof UIViewController *)viewController { |
| 158 | + [viewController willMoveToParentViewController:nil]; |
| 159 | + [viewController.view removeFromSuperview]; |
| 160 | + [viewController removeFromParentViewController]; |
| 161 | +} |
| 162 | + |
| 163 | +- (void)unfocused:(NSInteger)numberOfFocusetView { |
| 164 | + if (self.internalViewControllers.count == self.countPageInController) { |
| 165 | + if ([self.dataSource respondsToSelector:@selector(unfocusedViewController:)]) { |
| 166 | + [self.dataSource unfocusedViewController:self.internalViewControllers[numberOfFocusetView]]; |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +- (void)changeViewControllersWithNumber:(NSInteger) numberOfFocusetView { |
| 172 | + for (int i = 0; i < self.internalViewControllers.count; i++) { |
| 173 | + CGFloat originXForView = self.spaceHorizontal + (self.childVCSize.width + self.sizeBetweenVC) * (i - numberOfFocusetView); |
| 174 | + if (i == numberOfFocusetView) { |
| 175 | + self.internalViewControllers[i].view.frame = CGRectMake(originXForView, self.spaceVertical + self.OffsetOfHeightCentralVC, self.childVCSize.width, self.childVCSize.height ); |
| 176 | + } else { |
| 177 | + self.internalViewControllers[i].view.frame = CGRectMake(originXForView, self.spaceVertical, self.childVCSize.width, self.childVCSize.height); |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +@end |
0 commit comments