diff --git a/SLPagingView.podspec b/SLPagingView.podspec index c4cf26c..fe58a21 100644 --- a/SLPagingView.podspec +++ b/SLPagingView.podspec @@ -1,13 +1,13 @@ Pod::Spec.new do |s| - s.name = 'SLPagingView' - s.version = '0.0.5' - s.summary = 'Navigation bar system allowing to do a Tinder like or Twitter like' - s.homepage = 'https://github.com/StefanLage/SLPagingView' - s.license = 'MIT' - s.author = { 'StefanLage' => 'lagestfan@gmail.com' } - s.source = { :git => 'https://github.com/StefanLage/SLPagingView.git', :tag => "#{s.version}" } - s.source_files = 'SLPagingView/**/*.{h,m}' - s.requires_arc = true - s.platform = :ios, '7.0' - s.ios.deployment_target = '7.0' -end + s.name = 'SLPagingView' + s.version = '0.0.5' + s.summary = 'Navigation bar system allowing to do a Tinder like or Twitter like' + s.homepage = 'https://github.com/StefanLage/SLPagingView' + s.license = 'MIT' + s.author = { 'StefanLage' => 'lagestfan@gmail.com' } + s.source = { :git => 'https://github.com/StefanLage/SLPagingView.git', :tag => "#{s.version}" } + s.source_files = 'SLPagingView/**/*.{h,m}' + s.requires_arc = true + s.platform = :ios, '7.0' + s.ios.deployment_target = '7.0' +end \ No newline at end of file diff --git a/SLPagingView/SLPagingViewController.h b/SLPagingView/SLPagingViewController.h index 9a52aa5..9991fd8 100644 --- a/SLPagingView/SLPagingViewController.h +++ b/SLPagingView/SLPagingViewController.h @@ -55,10 +55,17 @@ typedef void(^SLPagingViewDidChanged)(NSInteger currentPage); * @param currentPage */ @property (nonatomic, copy) SLPagingViewDidChanged didChangedPage; + +/* + * Contains all child view controllers + * if initialized with controlles not views + */ +@property (nonatomic, strong) NSMutableDictionary *viewControllersDict; + /* * Contains all views displayed */ -@property (nonatomic, strong) NSMutableDictionary *viewControllers; +@property (nonatomic, strong) NSMutableDictionary *viewsDict; /* * Tint color of the page control */ diff --git a/SLPagingView/SLPagingViewController.m b/SLPagingView/SLPagingViewController.m index 98a2e3c..87acb1e 100644 --- a/SLPagingView/SLPagingViewController.m +++ b/SLPagingView/SLPagingViewController.m @@ -69,16 +69,10 @@ -(id)initWithNavBarItems:(NSArray*)items navBarBackground:(UIColor*)background v ctr.tag = i; [controllerKeys addObject:@(i)]; } - else if([[views objectAtIndex:i] isKindOfClass:UIViewController.class]){ - UIViewController *ctr = [views objectAtIndex:i]; - // Set the tag - ctr.view.tag = i; - [controllerKeys addObject:@(i)]; - } } // Number of keys equals number of controllers ? if(controllerKeys.count == views.count) - _viewControllers = [[NSMutableDictionary alloc] initWithObjects:views + _viewsDict = [[NSMutableDictionary alloc] initWithObjects:views forKeys:controllerKeys]; else{ // Something went wrong -> inform the client @@ -109,11 +103,12 @@ -(id)initWithNavBarControllers:(NSArray *)controllers showPageControl:(BOOL)addP -(id)initWithNavBarControllers:(NSArray *)controllers navBarBackground:(UIColor *)background showPageControl:(BOOL)addPageControl{ NSMutableArray *views = [[NSMutableArray alloc] initWithCapacity:controllers.count]; NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:controllers.count]; + NSMutableArray *viewControllers = [[NSMutableArray alloc] initWithCapacity:controllers.count]; for(int i =0; i self.indexSelected) { - [(UIViewController*)self.childViewControllers[self.indexSelected] performSelectorOnMainThread:selector - withObject:object - waitUntilDone:NO]; - } - else{ - [self.childViewControllers enumerateObjectsUsingBlock:^(UIViewController* ctr, NSUInteger idx, BOOL *stop) { - [ctr performSelectorOnMainThread:selector - withObject:object - waitUntilDone:NO]; - }]; - } -} - // Add a view as a navigationBarItem -(void)addNavigationItem:(UIView*)v tag:(int)tag{ CGFloat distance = (SCREEN_SIZE.width/2) - self.navigationSideItemsStyle; @@ -362,15 +326,16 @@ -(void)addNavigationItem:(UIView*)v tag:(int)tag{ -(void)setupPagingProcess{ // Make our ScrollView - CGRect frame = CGRectMake(0, 0, SCREEN_SIZE.width, self.view.bounds.size.height); - self.scrollView = [[UIScrollView alloc] initWithFrame:frame]; + CGRect frame = self.view.bounds; + UIScrollView *someScrollView = [[UIScrollView alloc] initWithFrame:frame]; + self.scrollView = someScrollView; self.scrollView.backgroundColor = [UIColor clearColor]; self.scrollView.pagingEnabled = YES; self.scrollView.showsHorizontalScrollIndicator = NO; self.scrollView.showsVerticalScrollIndicator = NO; self.scrollView.delegate = self; self.scrollView.bounces = NO; - [self.scrollView setContentInset:UIEdgeInsetsMake(0, 0, -80, 0)]; + //[self.scrollView setContentInset:UIEdgeInsetsMake(0, 0, -80, 0)]; [self.view addSubview:self.scrollView]; // Adds all views @@ -390,14 +355,13 @@ -(void)setupPagingProcess{ // Add all views -(void)addControllers{ - if(self.viewControllers - && self.viewControllers.count > 0){ - float width = SCREEN_SIZE.width * self.viewControllers.count; - float height = CGRectGetHeight(self.view.bounds) - CGRectGetHeight(self.navigationBarView.bounds); + if(self.viewsDict + && self.viewsDict.count > 0){ + float width = SCREEN_SIZE.width * self.viewsDict.count; + float height = CGRectGetHeight(self.scrollView.frame); self.scrollView.contentSize = (CGSize){width, height}; - __block int i = 0; // Sort all keys in ascending - NSArray *sortedIndexes = [self.viewControllers.allKeys sortedArrayUsingComparator:^NSComparisonResult(NSNumber *key1, NSNumber *key2) { + NSArray *sortedIndexes = [self.viewsDict.allKeys sortedArrayUsingComparator:^NSComparisonResult(NSNumber *key1, NSNumber *key2) { if ([key1 integerValue] > [key2 integerValue]) { return (NSComparisonResult)NSOrderedDescending; } @@ -408,49 +372,46 @@ -(void)addControllers{ }]; [sortedIndexes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { - UIView *v = self.viewControllers[@(idx)]; + UIView *v = self.viewsDict[@(idx)]; + UIViewController *vc = self.viewControllersDict[@(idx)];\ + if (vc) { + [vc willMoveToParentViewController:self]; + [self addChildViewController:vc]; + } [self.scrollView addSubview:v]; + if (vc) { + [vc didMoveToParentViewController:self]; + } if([self useAutoLayout:v]){ // Using AutoLayout v.translatesAutoresizingMaskIntoConstraints = NO; // Width constraint, half of parent view width - [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:v - attribute:NSLayoutAttributeWidth - relatedBy:NSLayoutRelationEqual - toItem:self.scrollView - attribute:NSLayoutAttributeWidth - multiplier:1.0 - constant:0]]; + [self.scrollView addConstraint: + [NSLayoutConstraint constraintWithItem:v + attribute:NSLayoutAttributeWidth + relatedBy:NSLayoutRelationEqual + toItem:self.scrollView + attribute:NSLayoutAttributeWidth + multiplier:1.0 + constant:0]]; // Height constraint, half of parent view height - [self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:v - attribute:NSLayoutAttributeHeight - relatedBy:NSLayoutRelationEqual - toItem:self.scrollView - attribute:NSLayoutAttributeHeight - multiplier:1.0 - constant:0]]; - UIView *previous = [self.viewControllers objectForKey:[NSNumber numberWithFloat:(idx - 1)]]; - if(previous) - // Distance constraint: set distance between previous view and the current one - [self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[previous]-0-[v]" - options:0 - metrics:nil - views:@{@"v" : v, @"previous" : previous}]]; - else - // First view - [self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[v]" - options:0 - metrics:nil - views:@{@"v" : v }]]; - // Oridnate constraint : set the space between the Top and the current view - [self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"V:|-%f-[v]", CGRectGetHeight(self.navigationBarView.frame)] - options:0 - metrics:nil - views:@{@"v" : v}]]; + [self.scrollView addConstraint: + [NSLayoutConstraint constraintWithItem:v + attribute:NSLayoutAttributeHeight + relatedBy:NSLayoutRelationEqual + toItem:self.scrollView + attribute:NSLayoutAttributeHeight + multiplier:1.0 + constant:0]]; + + [self.scrollView addConstraints: + [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-%f-[v]", self.view.frame.size.width * idx] + options:0 + metrics:nil + views:@{@"v" : v}]]; } else{ - v.frame = (CGRect){SCREEN_SIZE.width * i, 0, SCREEN_SIZE.width, CGRectGetHeight(self.view.frame)}; - i++; + v.frame = (CGRect){SCREEN_SIZE.width * idx, 0, SCREEN_SIZE.width, CGRectGetHeight(self.view.frame)}; } }]; } @@ -460,7 +421,7 @@ -(void)addControllers{ -(void)tapOnHeader:(UITapGestureRecognizer *)recognizer{ if(self.isUserInteraction){ // Get the wanted view - UIView *view = [self.viewControllers objectForKey:@(recognizer.view.tag)]; + UIView *view = [self.viewsDict objectForKey:@(recognizer.view.tag)]; [self.scrollView scrollRectToVisible:view.frame animated:YES]; } @@ -518,12 +479,7 @@ - (void)orientationChanged:(NSNotification *)notification{ -(void)sendNewIndex:(UIScrollView *)scrollView{ CGFloat xOffset = scrollView.contentOffset.x; - NSInteger oldIndex = self.indexSelected; self.indexSelected = ((int) roundf(xOffset) % (self.navigationBarView.subviews.count * (int)SCREEN_SIZE.width)) / SCREEN_SIZE.width; - if(oldIndex != self.indexSelected) - [self notifyControllers:NSSelectorFromString(@"viewDidDisappear:") - object:@(YES) - checkIndex:YES]; if(self.pageControl){ if (self.pageControl.currentPage != self.indexSelected) { @@ -536,22 +492,10 @@ -(void)sendNewIndex:(UIScrollView *)scrollView{ if(self.didChangedPage) self.didChangedPage(self.indexSelected); } - // Try to notify the controller concerned - [self notifyControllers:NSSelectorFromString(@"viewDidAppear:") - object:@(YES) - checkIndex:YES]; } #pragma mark - ScrollView delegate --(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ - // Try to notify the controller concerned - [self notifyControllers:NSSelectorFromString(@"viewWillDisappear:") - object:@(YES) - checkIndex:YES - ]; -} - - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // Update nav items [self updateNavItems:scrollView.contentOffset.x];