Skip to content

Commit

Permalink
fix gesture bug. enable system back gesture
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwangmodify committed Jan 20, 2017
1 parent 59d3db1 commit d23b22f
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 14 deletions.
4 changes: 4 additions & 0 deletions WXSTransition/CollectionViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ -(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"interactivePopGestureRecognizer - :%d",self.navigationController.interactivePopGestureRecognizer.isEnabled);
}
#pragma mark Delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 10;
Expand Down
2 changes: 1 addition & 1 deletion WXSTransition/SecondViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ @implementation SecondViewController

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = NO;
self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ - (void)wxs_pushViewController:(UIViewController *)viewController makeTransition
self.delegate = viewController;
viewController.wxs_addTransitionFlag = YES;
viewController.wxs_callBackTransition = transitionBlock ? transitionBlock : nil;

[self pushViewController:viewController animated:YES];
self.delegate = nil;
if (viewController.wxs_tempNavDelegate) {
self.delegate = viewController.wxs_tempNavDelegate;
}


}

- (UIViewController *)wxs_popViewControllerAnimated:(BOOL)animated {
Expand Down
2 changes: 0 additions & 2 deletions WXSTransition/WXSTransition/UIViewController+WXSTransition.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ typedef void(^WXSTransitionBlock)(WXSTransitionProperty *transition);
@interface UIViewController (WXSTransition) <UIViewControllerTransitioningDelegate,UINavigationControllerDelegate>




- (void)wxs_presentViewController:(UIViewController *)viewControllerToPresent animationType:(WXSTransitionAnimationType )animationType completion:(void (^)(void))completion;
- (void)wxs_presentViewController:(UIViewController *)viewControllerToPresent makeTransition:(WXSTransitionBlock)transitionBlock;
- (void)wxs_presentViewController:(UIViewController *)viewControllerToPresent makeTransition:(WXSTransitionBlock)transitionBlock completion:(void (^)(void))completion;
Expand Down
79 changes: 68 additions & 11 deletions WXSTransition/WXSTransition/UIViewController+WXSTransition.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,76 @@ @implementation UIViewController (WXSTransition)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{


Method method0 = class_getInstanceMethod(self.class, @selector(wxs_dismissViewControllerAnimated:completion:));
Method method1 = class_getInstanceMethod(self.class, @selector(dismissViewControllerAnimated:completion:));
method_exchangeImplementations(method0, method1);



SEL originalSelector = @selector(viewDidAppear:);
SEL swizzledSelector = @selector(wxs_viewDidAppear:);

Method originalMethod = class_getInstanceMethod(self.class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(self.class, swizzledSelector);

BOOL success = class_addMethod(self.class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (success) {
class_replaceMethod(self.class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}




originalSelector = @selector(viewWillDisappear:);
swizzledSelector = @selector(wxs_viewWillDisappear:);

originalMethod = class_getInstanceMethod(self.class, originalSelector);
swizzledMethod = class_getInstanceMethod(self.class, swizzledSelector);

success = class_addMethod(self.class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (success) {
class_replaceMethod(self.class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}

});
}



- (void)wxs_dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
if (self.wxs_delegateFlag) {
self.transitioningDelegate = self;
if (self.wxs_transitioningDelegate) {
self.transitioningDelegate = self.wxs_transitioningDelegate;
}
}
[self wxs_dismissViewControllerAnimated:flag completion:completion];
}

- (void)wxs_viewDidAppear:(BOOL)animated {

[self wxs_viewDidAppear:animated];
if (self.wxs_backGestureEnable) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
}


- (void)wxs_viewWillDisappear:(BOOL)animated {
[self wxs_viewWillDisappear:animated];
if (self.wxs_backGestureEnable) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
}



#pragma mark Action Method

//Default
Expand Down Expand Up @@ -62,16 +126,6 @@ -(void)wxs_presentViewController:(UIViewController *)viewControllerToPresent mak

}

- (void)wxs_dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion {
if (self.wxs_delegateFlag) {
self.transitioningDelegate = self;
if (self.wxs_transitioningDelegate) {
self.transitioningDelegate = self.wxs_transitioningDelegate;
}
}
[self wxs_dismissViewControllerAnimated:flag completion:completion];
}




Expand All @@ -88,6 +142,7 @@ - (void)wxs_dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))
self.wxs_callBackTransition ? self.wxs_callBackTransition(make) : nil;
_transtion = [WXSTransitionManager copyPropertyFromObjcet:make toObjcet:_transtion];
_transtion.transitionType = WXSTransitionTypeDismiss;
self.wxs_backGestureEnable = make.backGestureEnable;
return _transtion;

}
Expand All @@ -103,6 +158,7 @@ - (void)wxs_dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))
_transtion = [WXSTransitionManager copyPropertyFromObjcet:make toObjcet:_transtion];
_transtion.transitionType = WXSTransitionTypePresent;
self.wxs_delegateFlag = _transtion.isSysBackAnimation ? NO : YES;
self.wxs_backGestureEnable = make.backGestureEnable;
return _transtion;

}
Expand Down Expand Up @@ -134,6 +190,7 @@ - (void)wxs_dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))
if ( operation == UINavigationControllerOperationPush ) {
self.wxs_delegateFlag = _transtion.isSysBackAnimation ? NO : YES;
_transtion.transitionType = WXSTransitionTypePush;

}else{
_transtion.transitionType = WXSTransitionTypePop;
}
Expand All @@ -150,10 +207,10 @@ - (void)wxs_dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))
};

}
self.wxs_backGestureEnable = make.backGestureEnable;
return _transtion;

}

- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController {

if (!self.wxs_addTransitionFlag) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef void(^WXSTransitionBlock)(WXSTransitionProperty *transition);
@property (nonatomic, copy ) WXSTransitionBlock wxs_callBackTransition;
@property (nonatomic, assign) BOOL wxs_delegateFlag;
@property (nonatomic, assign) BOOL wxs_addTransitionFlag;
@property (nonatomic, assign) BOOL wxs_backGestureEnable;

@property (nonatomic, weak ) id wxs_transitioningDelegate;
@property (nonatomic, weak ) id wxs_tempNavDelegate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
static NSString *wxs_callBackTransitionKey = @"CallBackTransitionKey";
static NSString *wxs_delegateFlagKey = @"wxs_DelegateFlagKey";
static NSString *wxs_addTransitionFlagKey = @"wxs_addTransitionFlagKey";
static NSString *wxs_backGestureEnableKey = @"wxs_backGestureEnableKey";
static NSString *wxs_transitioningDelegateKey = @"wxs_transitioningDelegateKey";
static NSString *wxs_tempNavDelegateKey = @"wxs_tempNavDelegateKey";

Expand Down Expand Up @@ -49,6 +50,16 @@ - (BOOL)wxs_addTransitionFlag {
return [objc_getAssociatedObject(self, &wxs_addTransitionFlagKey) integerValue] == 0 ? NO : YES;
}


// ---- wxs_backGestureEnable
- (void)setWxs_backGestureEnable:(BOOL)wxs_backGestureEnable {
objc_setAssociatedObject(self, &wxs_backGestureEnableKey, @(wxs_backGestureEnable), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)wxs_backGestureEnable {
return [objc_getAssociatedObject(self , &wxs_backGestureEnableKey) integerValue] == 0 ? NO : YES;
}

//----- Wxs_transitioningDelega
- (void)setWxs_transitioningDelegate:(id)wxs_transitioningDelegate {
objc_setAssociatedObject(self, &wxs_transitioningDelegateKey, wxs_transitioningDelegate, OBJC_ASSOCIATION_ASSIGN);
Expand Down

0 comments on commit d23b22f

Please sign in to comment.