Skip to content
This repository was archived by the owner on Apr 16, 2021. It is now read-only.

add a new function #4

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
13 changes: 11 additions & 2 deletions Classes/FlipBoardNavigationController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@
//

#import <UIKit/UIKit.h>
#import "UIView+Util.h"

typedef void (^FlipBoardNavigationControllerCompletionBlock)(void);

@interface FlipBoardNavigationController : UIViewController

{
CGPoint _rightPanViewCenterPoint;
BOOL _originTouchProcess;
BOOL _rightTouchProcess;

UIView * _view_mask_fake;
}
@property(nonatomic, retain) NSMutableArray *viewControllers;

@property (nonatomic,strong) UIViewController * rightPanController;
-(void)addRightPanViewController:(UIViewController*)viewController;
- (id) initWithRootViewController:(UIViewController*)rootViewController;

- (void) pushViewController:(UIViewController *)viewController;
- (void) pushViewController:(UIViewController *)viewController completion:(FlipBoardNavigationControllerCompletionBlock)handler;
- (void) popViewController;
- (void) popViewControllerWithCompletion:(FlipBoardNavigationControllerCompletionBlock)handler;
- (void) popViewController:(UIViewController*)viewControler withCompletion:(FlipBoardNavigationControllerCompletionBlock)handler;
@end

@interface UIViewController (FlipBoardNavigationController)
Expand Down
159 changes: 143 additions & 16 deletions Classes/FlipBoardNavigationController.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
static const CGFloat kAnimationDelay = 0.0f;
static const CGFloat kOffsetTrigger = 30.0f;
static const CGFloat kMaxBlackMaskAlpha = 0.8f;
static const CGFloat kViewScaleSize = 0.95f;


typedef enum {
Expand Down Expand Up @@ -67,8 +68,13 @@ - (void) loadView {
_blackMask.backgroundColor = [UIColor blackColor];
_blackMask.alpha = 0.0;
[self.view insertSubview:_blackMask atIndex:0];

}
-(void)addRightPanViewController:(UIViewController*)viewController
{
[self.view addSubview:viewController.view];
[self addPanGestureToView:viewController.view];
}

#pragma mark - PushViewController With Completion Block
- (void) pushViewController:(UIViewController *)viewController completion:(FlipBoardNavigationControllerCompletionBlock)handler {
_animationInProgress = YES;
Expand All @@ -80,7 +86,7 @@ - (void) pushViewController:(UIViewController *)viewController completion:(FlipB
[self.view addSubview:viewController.view];
[UIView animateWithDuration:kAnimationDuration delay:kAnimationDelay options:0 animations:^{
CGAffineTransform transf = CGAffineTransformIdentity;
[self currentViewController].view.transform = CGAffineTransformScale(transf, 0.9f, 0.9f);
[self currentViewController].view.transform = CGAffineTransformScale(transf, kViewScaleSize, kViewScaleSize);
viewController.view.frame = self.view.bounds;
_blackMask.alpha = kMaxBlackMaskAlpha;
} completion:^(BOOL finished) {
Expand Down Expand Up @@ -119,6 +125,7 @@ - (void) popViewControllerWithCompletion:(FlipBoardNavigationControllerCompletio
[self.view bringSubviewToFront:[self previousViewController].view];
[currentVC removeFromParentViewController];
[currentVC didMoveToParentViewController:nil];
[currentVC.view removeFromSuperview];///修正view不能释放 jack 2013-7-2
[self.viewControllers removeObject:currentVC];
_animationInProgress = NO;
handler();
Expand All @@ -127,6 +134,36 @@ - (void) popViewControllerWithCompletion:(FlipBoardNavigationControllerCompletio

}

- (void) popViewController:(UIViewController*)viewControler withCompletion:(FlipBoardNavigationControllerCompletionBlock)handler
{
BOOL exist = NO;
for (UIViewController * subControler in self.viewControllers)
{
if(subControler==viewControler)
{
exist = YES;
break;
}
}
if (self.viewControllers.count < 2||!exist) {
return;
}

UIViewController *currentVC = [self currentViewController];
if(currentVC==viewControler)
{
[self popViewControllerWithCompletion:handler];
return;
}

[viewControler willMoveToParentViewController:nil];
[viewControler removeFromParentViewController];
[viewControler didMoveToParentViewController:nil];
[viewControler.view removeFromSuperview];
[self.viewControllers removeObject:viewControler];
handler();
}

- (void) popViewController {
[self popViewControllerWithCompletion:^{}];
}
Expand All @@ -140,7 +177,7 @@ - (void) rollBackViewController {
self.view.transform = self.view.transform;
[UIView animateWithDuration:((vc.view.frame.origin.x *kAnimationDuration)/self.view.frame.size.width) delay:kAnimationDelay options:0 animations:^{
CGAffineTransform transf = CGAffineTransformIdentity;
nvc.view.transform = CGAffineTransformScale(transf, 0.9f, 0.9f);
nvc.view.transform = CGAffineTransformScale(transf, kViewScaleSize, kViewScaleSize);
vc.view.frame = rect;
_blackMask.alpha = kMaxBlackMaskAlpha;
} completion:^(BOOL finished) {
Expand Down Expand Up @@ -179,34 +216,131 @@ - (void) addPanGestureToView:(UIView*)view
UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(gestureRecognizerDidPan:)];
panGesture.cancelsTouchesInView = YES;
panGesture.maximumNumberOfTouches = 1;
panGesture.delegate = self;
[view addGestureRecognizer:panGesture];
[_gestures addObject:panGesture];
panGesture = nil;
}

}

#pragma mark - Gesture recognizer
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
UIViewController * vc = [self.viewControllers lastObject];
_panOrigin = vc.view.frame.origin;
gestureRecognizer.enabled = YES;
_rightPanViewCenterPoint = self.rightPanController.view.center;
return !_animationInProgress;
}

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
return NO;
}

-(void)rightPanViewControllerAnimationEndWithDirection:(PanDirection)direction
{
_animationInProgress = YES;
CGRect frame = CGRectZero;
switch (direction)
{
case PanDirectionLeft:
break;
case PanDirectionRight:
frame.origin = CGPointMake(320, 0);
default:
break;
}
frame.size = self.rightPanController.view.frame.size;

[UIView animateWithDuration:((self.rightPanController.view.frame.origin.x *kAnimationDuration)/self.view.frame.size.width)
animations:^()
{
if(direction==PanDirectionRight)
{
[self.view exchangeSubview:[self currentViewController].view
withSubview:_blackMask];
}
self.rightPanController.view.frame =frame;
CGFloat newAlpha = direction==PanDirectionLeft?kMaxBlackMaskAlpha:0.0;
_blackMask.alpha = newAlpha;

CGAffineTransform transf = CGAffineTransformIdentity;
CGFloat newTransformValue = direction==PanDirectionLeft?kViewScaleSize:1.0;
[self currentViewController].view.transform = CGAffineTransformScale(transf,newTransformValue,newTransformValue);
if(direction==PanDirectionRight)
[self currentViewController].view.userInteractionEnabled = YES;
_animationInProgress = NO;
}];
}

- (void) gestureRecognizerDidPan:(UIPanGestureRecognizer*)panGesture {
if(_animationInProgress) return;
CGPoint currentPoint = [panGesture translationInView:self.view];
CGFloat x = currentPoint.x + _panOrigin.x;
CGFloat offset = 0;

UIViewController * vc ;
vc = [self currentViewController];
PanDirection panDirection = PanDirectionNone;
CGPoint vel = [panGesture velocityInView:self.view];
if (vel.x > kOffsetTrigger) {
panDirection = PanDirectionRight;
} else {
panDirection = PanDirectionLeft;
}

BOOL moveLeft = vel.x<1;
UIViewController * vc= [self currentViewController];

if(self.rightPanController&&self.rightPanController.view.frame.origin.x==0&&moveLeft)
return;
if(self.rightPanController&&//是否存在右侧的panViewController
((moveLeft&&_rightPanController.view.center.x>160)||//向左滑入rightPanView
(!moveLeft&&_rightPanController.view.center.x<480))&&//向右推出rightPanView
!_originTouchProcess)//当前是否正在处理原有的touch事件
{
if([self.view.subviews indexOfObject:self.currentViewController.view]>
[self.view.subviews indexOfObject:_blackMask])
{
[self.view exchangeSubview:[self currentViewController].view
withSubview:_blackMask];
}
[self currentViewController].view.userInteractionEnabled = NO;
_rightTouchProcess = YES;
vc = self.rightPanController;
CGPoint center = CGPointMake(_rightPanViewCenterPoint.x+currentPoint.x, vc.view.center.y);

offset = CGRectGetWidth(self.rightPanController.view.frame) - x;
CGAffineTransform transf = CGAffineTransformIdentity;
CGFloat percentValue = abs(currentPoint.x)/320.0;
CGFloat newAlphaValue = percentValue* kMaxBlackMaskAlpha;
BOOL rightPanIsShow = _rightPanViewCenterPoint.x==160;
newAlphaValue = rightPanIsShow?1-newAlphaValue:newAlphaValue;
CGFloat newTransformValueFRTL = 1-(1-kViewScaleSize)*percentValue;
CGFloat newTransformValueFLTR = kViewScaleSize+(1-kViewScaleSize)*percentValue;

CGFloat newTransformValue = rightPanIsShow?newTransformValueFLTR:newTransformValueFRTL;
[self currentViewController].view.transform = CGAffineTransformScale(transf,newTransformValue,newTransformValue);
_blackMask.alpha = newAlphaValue;

if(center.x<160)
{
vc.view.center = CGPointMake(160, vc.view.center.y);
[self currentViewController].view.userInteractionEnabled = YES;
return;
}
vc.view.center = center;

}
if(_rightTouchProcess)
{
if (panGesture.state == UIGestureRecognizerStateEnded ||
panGesture.state == UIGestureRecognizerStateCancelled){
panDirection = moveLeft?PanDirectionLeft:PanDirectionRight;
_rightTouchProcess = NO;
[self rightPanViewControllerAnimationEndWithDirection:panDirection];

}
return;
}
_originTouchProcess = YES;
offset = CGRectGetWidth(vc.view.frame) - x;
vc.view.frame = [self getSlidingRectForOffset:offset];

Expand All @@ -218,15 +352,8 @@ - (void) gestureRecognizerDidPan:(UIPanGestureRecognizer*)panGesture {

_blackMask.alpha = newAlphaValue;

PanDirection panDirection = PanDirectionNone;
CGPoint vel = [panGesture velocityInView:self.view];
if (vel.x > kOffsetTrigger) {
panDirection = PanDirectionRight;
} else {
panDirection = PanDirectionLeft;
}

if (panGesture.state == UIGestureRecognizerStateEnded || panGesture.state == UIGestureRecognizerStateCancelled) {
_originTouchProcess = NO;
[self completeSlidingAnimationWithDirection:panDirection];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
2849886C176325DA0079F801 /* UIView+Util.m in Sources */ = {isa = PBXBuildFile; fileRef = 2849886B176325DA0079F801 /* UIView+Util.m */; };
99B0A1061731743800253408 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 99B0A1051731743800253408 /* UIKit.framework */; };
99B0A1081731743800253408 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 99B0A1071731743800253408 /* Foundation.framework */; };
99B0A10A1731743800253408 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 99B0A1091731743800253408 /* CoreGraphics.framework */; };
Expand Down Expand Up @@ -43,6 +44,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
2849886A176325DA0079F801 /* UIView+Util.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+Util.h"; sourceTree = "<group>"; };
2849886B176325DA0079F801 /* UIView+Util.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+Util.m"; sourceTree = "<group>"; };
99B0A1021731743800253408 /* FlipViewControllerDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = FlipViewControllerDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
99B0A1051731743800253408 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
99B0A1071731743800253408 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -139,6 +142,8 @@
99B0A143173174FB00253408 /* Classes */,
99B0A1141731743800253408 /* AppDelegate.h */,
99B0A1151731743800253408 /* AppDelegate.m */,
2849886A176325DA0079F801 /* UIView+Util.h */,
2849886B176325DA0079F801 /* UIView+Util.m */,
99B0A11D1731743800253408 /* MainStoryboard_iPhone.storyboard */,
99B0A1201731743800253408 /* MainStoryboard_iPad.storyboard */,
99B0A1231731743800253408 /* ViewController.h */,
Expand Down Expand Up @@ -216,6 +221,7 @@
99B0A0FE1731743800253408 /* Sources */,
99B0A0FF1731743800253408 /* Frameworks */,
99B0A1001731743800253408 /* Resources */,
285BE8241761B85D00250AC9 /* ShellScript */,
);
buildRules = (
);
Expand Down Expand Up @@ -300,6 +306,19 @@
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
285BE8241761B85D00250AC9 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "echo \"$CODESIGNING_FOLDER_PATH\" >/tmp/\"$USER.ident\" && echo \"$CODE_SIGN_IDENTITY\" >>/tmp/\"$USER.ident\" && exit;";
};
99B0A1291731743800253408 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand All @@ -326,6 +345,7 @@
99B0A146173174FB00253408 /* FlipBoardNavigationController.m in Sources */,
99B0A15017317AF000253408 /* ChildViewController.m in Sources */,
99B0A15317317C7400253408 /* AnotherViewController.m in Sources */,
2849886C176325DA0079F801 /* UIView+Util.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

UIButton * btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn addTarget:self
action:@selector(popController:)
forControlEvents:UIControlEventTouchUpInside];
btn.frame =CGRectMake(100, 100, 100, 100);
[self.view addSubview:btn];
}

-(void)popController:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"TTT"
object:nil];
}

- (void)didReceiveMemoryWarning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(test:)
name:@"TTT"
object:nil];
}

-(void)test:(id)sender
{
[self.flipboardNavigationController popViewController:self
withCompletion:^()
{
NSLog(@"finish");
[[NSNotificationCenter defaultCenter] removeObserver:nil];
}];
}

- (void)didReceiveMemoryWarning
Expand Down
14 changes: 14 additions & 0 deletions Sample/FlipViewControllerDemo/FlipViewControllerDemo/UIView+Util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// UIView+Util.h
// FlipViewControllerDemo
//
// Created by demon on 6/8/13.
// Copyright (c) 2013 Michael Henry Pantaleon. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIView (Util)
-(void)exchangeSubview:(UIView*)view1
withSubview:(UIView*)view2;
@end
Loading