From 5abd7c9cd65a874eb16ed9e37beeccdc4fd3d17f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD=20=D0=94=D0=BE=D0=BC=D0=B0?= =?UTF-8?q?=D1=88=D0=BD=D0=B5=D0=B2?= Date: Fri, 13 Sep 2013 14:09:30 +0400 Subject: [PATCH 01/12] Add more control for pan gesture recogniser from visible viewController; --- Classes/FlipBoardNavigationController.h | 1 + Classes/FlipBoardNavigationController.m | 43 ++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/Classes/FlipBoardNavigationController.h b/Classes/FlipBoardNavigationController.h index 173931a..833b9fe 100644 --- a/Classes/FlipBoardNavigationController.h +++ b/Classes/FlipBoardNavigationController.h @@ -43,6 +43,7 @@ typedef void (^FlipBoardNavigationControllerCompletionBlock)(void); @interface UIViewController (FlipBoardNavigationController) @property (nonatomic, retain) FlipBoardNavigationController *flipboardNavigationController; +@property (nonatomic, readonly) UIPanGestureRecognizer *flipboardNavigationControllerPanGesture; @end diff --git a/Classes/FlipBoardNavigationController.m b/Classes/FlipBoardNavigationController.m index 7084e16..e10bc53 100644 --- a/Classes/FlipBoardNavigationController.m +++ b/Classes/FlipBoardNavigationController.m @@ -27,6 +27,7 @@ #import "FlipBoardNavigationController.h" #import +#import static const CGFloat kAnimationDuration = 0.5f; static const CGFloat kAnimationDelay = 0.0f; @@ -38,6 +39,11 @@ PanDirectionRight = 2 } PanDirection; +@interface UIViewController() + +@property (nonatomic, retain, readwrite) UIPanGestureRecognizer *flipboardNavigationControllerPanGesture; + +@end @interface FlipBoardNavigationController (){ NSMutableArray *_gestures; @@ -47,7 +53,7 @@ @interface FlipBoardNavigationController (){ CGFloat _percentageOffsetFromLeft; } -- (void) addPanGestureToView:(UIView*)view; +- (void) addPanGestureToViewController:(UIViewController*)viewController; - (void) rollBackViewController; - (UIViewController *)currentViewController; @@ -121,7 +127,7 @@ - (void) pushViewController:(UIViewController *)viewController completion:(FlipB [viewController didMoveToParentViewController:self]; _animationInProgress = NO; _gestures = [[NSMutableArray alloc] init]; - [self addPanGestureToView:[self currentViewController].view]; + [self addPanGestureToViewController:[self currentViewController]]; handler(); } }]; @@ -215,14 +221,15 @@ - (UIViewController *)previousViewController { } #pragma mark - Add Pan Gesture -- (void) addPanGestureToView:(UIView*)view +- (void) addPanGestureToViewController:(UIViewController*)viewController { NSLog(@"ADD PAN GESTURE $$### %i",[_gestures count]); UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(gestureRecognizerDidPan:)]; panGesture.cancelsTouchesInView = YES; panGesture.delegate = self; - [view addGestureRecognizer:panGesture]; + viewController.flipboardNavigationControllerPanGesture = panGesture; + [viewController.view addGestureRecognizer:panGesture]; [_gestures addObject:panGesture]; panGesture = nil; } @@ -230,7 +237,18 @@ - (void) addPanGestureToView:(UIView*)view # pragma mark - Avoid Unwanted Vertical Gesture - (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer { CGPoint translation = [panGestureRecognizer translationInView:self.view]; - return fabs(translation.x) > fabs(translation.y) ; + if(fabs(translation.x) > fabs(translation.y)){ + UIViewController *currentViewController = [self currentViewController]; + if([currentViewController respondsToSelector:@selector(gestureRecognizerShouldBegin:)]){ + return [(UIViewController *)currentViewController gestureRecognizerShouldBegin:panGestureRecognizer]; + } + else{ + return YES; + } + } + else{ + return NO; + } } #pragma mark - Gesture recognizer @@ -242,6 +260,10 @@ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceive } - (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { + UIViewController *currentViewController = [self currentViewController]; + if([currentViewController respondsToSelector:@selector(gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:)]){ + return [(UIViewController *)currentViewController gestureRecognizer:gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:otherGestureRecognizer]; + } return YES; } @@ -341,10 +363,21 @@ - (CGRect) viewBoundsWithOrientation:(UIInterfaceOrientation)orientation{ #pragma mark - UIViewController Category + +NSString* const FlipboardNavigationControllerPanGesture = @"FlipboardNavigationControllerPanGesture"; + //For Global Access of flipViewController @implementation UIViewController (FlipBoardNavigationController) @dynamic flipboardNavigationController; +- (void)setFlipboardNavigationControllerPanGesture:(UIPanGestureRecognizer *)flipboardNavigationControllerPanGesture{ + objc_setAssociatedObject(self, (__bridge const void *)(FlipboardNavigationControllerPanGesture), flipboardNavigationControllerPanGesture, OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +- (UIPanGestureRecognizer *)flipboardNavigationControllerPanGesture{ + return objc_getAssociatedObject(self, (__bridge const void *)(FlipboardNavigationControllerPanGesture)); +} + - (FlipBoardNavigationController *)flipboardNavigationController { From 23ac3eb57312410af3c8af5374533666cc36b197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD=20=D0=94=D0=BE=D0=BC=D0=B0?= =?UTF-8?q?=D1=88=D0=BD=D0=B5=D0=B2?= Date: Fri, 13 Sep 2013 17:05:37 +0400 Subject: [PATCH 02/12] Add popToRootViewControllerMethod; --- Classes/FlipBoardNavigationController.h | 7 ++- Classes/FlipBoardNavigationController.m | 47 ++++++++++++++++++- .../AnotherViewController.m | 5 ++ .../en.lproj/MainStoryboard_iPad.storyboard | 46 +++--------------- .../en.lproj/MainStoryboard_iPhone.storyboard | 26 +++------- 5 files changed, 70 insertions(+), 61 deletions(-) diff --git a/Classes/FlipBoardNavigationController.h b/Classes/FlipBoardNavigationController.h index 833b9fe..4f087da 100644 --- a/Classes/FlipBoardNavigationController.h +++ b/Classes/FlipBoardNavigationController.h @@ -31,7 +31,9 @@ typedef void (^FlipBoardNavigationControllerCompletionBlock)(void); @interface FlipBoardNavigationController : UIViewController -@property(nonatomic, retain) NSMutableArray *viewControllers; +@property (nonatomic, copy) float(^alphaCalculationBlock)(CGFloat transition); +@property (nonatomic, assign) NSTimeInterval transitionsAnimationDuration; +@property (nonatomic, retain) NSMutableArray *viewControllers; - (id) initWithRootViewController:(UIViewController*)rootViewController; @@ -39,6 +41,9 @@ typedef void (^FlipBoardNavigationControllerCompletionBlock)(void); - (void) pushViewController:(UIViewController *)viewController completion:(FlipBoardNavigationControllerCompletionBlock)handler; - (void) popViewController; - (void) popViewControllerWithCompletion:(FlipBoardNavigationControllerCompletionBlock)handler; +- (void) popToRootViewController; +- (void) popToRootViewControllerWithCompletion:(FlipBoardNavigationControllerCompletionBlock)handler; + @end @interface UIViewController (FlipBoardNavigationController) diff --git a/Classes/FlipBoardNavigationController.m b/Classes/FlipBoardNavigationController.m index e10bc53..e59c0b6 100644 --- a/Classes/FlipBoardNavigationController.m +++ b/Classes/FlipBoardNavigationController.m @@ -169,6 +169,43 @@ - (void) popViewControllerWithCompletion:(FlipBoardNavigationControllerCompletio } +- (void) popToRootViewController{ + [self popToRootViewControllerWithCompletion:nil]; +} + +- (void) popToRootViewControllerWithCompletion:(FlipBoardNavigationControllerCompletionBlock)handler{ + + _animationInProgress = YES; + if (self.viewControllers.count < 2) { + return; + } + + UIViewController *currentVC = [self currentViewController]; + UIViewController *rootVC = [self rootViewController]; + if(![currentVC isEqual: rootVC]){ + [rootVC viewWillAppear:NO]; + [UIView animateWithDuration:kAnimationDuration delay:kAnimationDelay options:0 animations:^{ + currentVC.view.frame = CGRectOffset(self.view.bounds, self.view.bounds.size.width, 0); + CGAffineTransform transf = CGAffineTransformIdentity; + rootVC.view.transform = CGAffineTransformScale(transf, 1.0, 1.0); + rootVC.view.frame = self.view.bounds; + _blackMask.alpha = 0.0; + } completion:^(BOOL finished) { + if (finished) { + [currentVC.view removeFromSuperview]; + [currentVC willMoveToParentViewController:nil]; + [self.view bringSubviewToFront:[self previousViewController].view]; + [currentVC removeFromParentViewController]; + [currentVC didMoveToParentViewController:nil]; + [_viewControllers removeObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, [_viewControllers count] - 1)]]; + _animationInProgress = NO; + [rootVC viewDidAppear:NO]; + handler(); + } + }]; + } +} + - (void) popViewController { [self popViewControllerWithCompletion:^{}]; } @@ -220,6 +257,14 @@ - (UIViewController *)previousViewController { return result; } +- (UIViewController *)rootViewController { + UIViewController *result = nil; + if ([self.viewControllers count]>0) { + result = [self.viewControllers objectAtIndex:0]; + } + return result; +} + #pragma mark - Add Pan Gesture - (void) addPanGestureToViewController:(UIViewController*)viewController { @@ -307,7 +352,7 @@ - (void) gestureRecognizerDidPan:(UIPanGestureRecognizer*)panGesture { - (void) transformAtPercentage:(CGFloat)percentage { CGAffineTransform transf = CGAffineTransformIdentity; CGFloat newTransformValue = 1 - (percentage*10)/100; - CGFloat newAlphaValue = percentage* kMaxBlackMaskAlpha; + CGFloat newAlphaValue = (self.alphaCalculationBlock != nil) ? self.alphaCalculationBlock(percentage) : percentage* kMaxBlackMaskAlpha; [self previousViewController].view.transform = CGAffineTransformScale(transf,newTransformValue,newTransformValue); _blackMask.alpha = newAlphaValue; } diff --git a/Sample/FlipViewControllerDemo/FlipViewControllerDemo/AnotherViewController.m b/Sample/FlipViewControllerDemo/FlipViewControllerDemo/AnotherViewController.m index 4c700ba..c8d49fe 100644 --- a/Sample/FlipViewControllerDemo/FlipViewControllerDemo/AnotherViewController.m +++ b/Sample/FlipViewControllerDemo/FlipViewControllerDemo/AnotherViewController.m @@ -7,6 +7,7 @@ // #import "AnotherViewController.h" +#import "FlipBoardNavigationController.h" @interface AnotherViewController () @@ -35,4 +36,8 @@ - (void)didReceiveMemoryWarning // Dispose of any resources that can be recreated. } +- (void)popToRootClicked:(id)sender{ + [self.flipboardNavigationController popToRootViewController]; +} + @end diff --git a/Sample/FlipViewControllerDemo/FlipViewControllerDemo/en.lproj/MainStoryboard_iPad.storyboard b/Sample/FlipViewControllerDemo/FlipViewControllerDemo/en.lproj/MainStoryboard_iPad.storyboard index f26e873..94bdcf2 100644 --- a/Sample/FlipViewControllerDemo/FlipViewControllerDemo/en.lproj/MainStoryboard_iPad.storyboard +++ b/Sample/FlipViewControllerDemo/FlipViewControllerDemo/en.lproj/MainStoryboard_iPad.storyboard @@ -1,7 +1,8 @@ - + - + + @@ -9,20 +10,15 @@ - +