Skip to content

Commit

Permalink
【侧滑返回手势优化处理】
Browse files Browse the repository at this point in the history
  • Loading branch information
kingsic committed Sep 1, 2018
1 parent 32840e5 commit f304f4c
Show file tree
Hide file tree
Showing 16 changed files with 177 additions and 164 deletions.
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,18 @@ SGPageContentCollectionView(内部由 UICollectionView 实现)


## 问题及解决方案
#### 1、CocoaPods 安装 SGPagingView 时,遇到的问题及解决方案
### 、CocoaPods 安装 SGPagingView 时,遇到的问题及解决方案
* 若在使用 CocoaPods 安装 SGPagingView 时,出现 [!] Unable to find a specification for SGPagingView 提示时,打开终端先输入 pod repo remove master;执行完毕后再输入 pod setup 即可 (可能会等待一段时间)
***

#### 2、关于父子控制器的说明(SGPageContentScrollView 与 SGPageContentCollectionView)
### 、关于父子控制器的说明(SGPageContentScrollView 与 SGPageContentCollectionView)
###### 参考链接
* [添加子视图控制器时,子视图控制器的 viewWillAppear 方法不调用](https://blog.csdn.net/u012907783/article/details/78972227)
* [addChildViewController 与 viewWillAppear、viewDidAppear 关系说明](https://blog.csdn.net/zhaoxy_thu/article/details/50826190)
***

#### 3、关于侧滑返回手势(请参考 DefaultVCPopGesture 类以及点击子控制器对下一界面所做的处理)
###### 1、如果是系统默认返回 item ;只需实现 SGPageContentScrollView 的 pageContentScrollView:offsetX:代理方法或 SGPageContentCollectionView 的 pageContentCollectionView:offsetX:代理方法,并在此方法实现以下代码即可,如:
### 、关于侧滑返回手势(请参考 DefaultVCPopGesture 类以及点击子控制器对下一界面所做的处理)
#### 1、如果是系统默认返回 item ;只需实现 SGPageContentScrollView 的 pageContentScrollView:offsetX:代理方法或 SGPageContentCollectionView 的 pageContentCollectionView:offsetX:代理方法,并在此方法实现以下代码即可,如:
```
- (void)pageContentScrollView:(SGPageContentScrollView *)pageContentScrollView index:(NSInteger)index {
if (index == 0) {
Expand All @@ -139,20 +139,27 @@ SGPageContentCollectionView(内部由 UICollectionView 实现)
}
```

###### 2、如果是自定义返回 item
a. 设置代理:self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
#### 2、如果是自定义返回 item
a. .h 文件中 #import "SGPagingViewPopGestureVC.h",控制器继承 SGPagingViewPopGestureVC;

b. 遵循协议:UINavigationControllerDelegate

c. 实现代理方法:
b. 实现 SGPageContentScrollView 的 pageContentScrollView:index:代理方法或 SGPageContentCollectionView 的 pageContentCollectionView:index:代理方法
```
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
- (void)pageContentScrollView:(SGPageContentScrollView *)pageContentScrollView index:(NSInteger)index {
if (index == 0) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
} else {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
}
```
d. 实现 SGPageContentScrollView 的 pageContentScrollView:index:代理方法或 SGPageContentCollectionView 的 pageContentCollectionView:index:代理方法;实现代码如 1、

###### 3、issues [关于返回手势](https://github.com/kingsic/SGPagingView/issues/25) 已有开发者提供了解决方案,可供参看
##### 温馨提示一:由于 index != 0 时 self.navigationController.interactivePopGestureRecognizer.enabled = NO; 所以 push 到下一个控制器需在 viewDidLoad 中设置为 YES;

##### 温馨提示二:导航栏包装自定义返回 item;使用 SGPagingView 也会导致侧滑返回失效解决方案同(2、如果是自定义返回 item)如果你工程中某个控制器使用自定义返回 item 解决方案同(2、如果是自定义返回 item);这里只是提供一种解决方案,仅供参考。如果你有更好的解决方案欢迎联我

##### 温馨提示三:自定义返回 item 导致侧滑返回手势失效[参考链接](https://www.jianshu.com/p/33ce1340a543)

#### 3、issues [关于返回手势](https://github.com/kingsic/SGPagingView/issues/25) 已有开发者提供了解决方案,仅供参看
***


Expand Down
13 changes: 13 additions & 0 deletions SGPagingView/Controller/SGPagingViewPopGestureVC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// SGPagingViewPopGestureVC.h
// SGPagingViewExample
//
// Created by kingsic on 2018/9/1.
// Copyright © 2018年 kingsic. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SGPagingViewPopGestureVC : UIViewController

@end
45 changes: 45 additions & 0 deletions SGPagingView/Controller/SGPagingViewPopGestureVC.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// SGPagingViewPopGestureVC.m
// SGPagingViewExample
//
// Created by kingsic on 2018/9/1.
// Copyright © 2018年 kingsic. All rights reserved.
//

#import "SGPagingViewPopGestureVC.h"

@interface SGPagingViewPopGestureVC () <UIGestureRecognizerDelegate> {
id<UIGestureRecognizerDelegate> _delegate;
}
@end

@implementation SGPagingViewPopGestureVC

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

if (self.navigationController.viewControllers.count > 1) {
// 记录系统返回手势的代理
_delegate = self.navigationController.interactivePopGestureRecognizer.delegate;
// 设置系统返回手势的代理为当前控制器
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

// 设置系统返回手势的代理为我们刚进入控制器的时候记录的系统的返回手势代理
self.navigationController.interactivePopGestureRecognizer.delegate = _delegate;
}

#pragma mark - - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
return self.navigationController.childViewControllers.count > 1;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return self.navigationController.viewControllers.count > 1;
}

@end
30 changes: 20 additions & 10 deletions SGPagingViewExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
8D0F8E721FD95E2300F30426 /* ChildVCTwo.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D0F8E5D1FD95E2300F30426 /* ChildVCTwo.m */; };
8D0F8E731FD95E2300F30426 /* ChildVCTwo.xib in Resources */ = {isa = PBXBuildFile; fileRef = 8D0F8E5E1FD95E2300F30426 /* ChildVCTwo.xib */; };
8D1A05F820B441B800743EAD /* DefaultAttributedTitleVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D1A05F720B441B800743EAD /* DefaultAttributedTitleVC.m */; };
8D1D282521114426009B3DF4 /* ChhildTempPopGestureVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D1D282321114426009B3DF4 /* ChhildTempPopGestureVC.m */; };
8D1D282621114426009B3DF4 /* ChhildTempPopGestureVC.xib in Resources */ = {isa = PBXBuildFile; fileRef = 8D1D282421114426009B3DF4 /* ChhildTempPopGestureVC.xib */; };
8D5BB745213AC303000E929E /* ChildTempPopGestureVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D5BB744213AC303000E929E /* ChildTempPopGestureVC.m */; };
8D5BB749213AC570000E929E /* SGPagingViewPopGestureVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D5BB747213AC570000E929E /* SGPagingViewPopGestureVC.m */; };
8D5E68CF20F2FA140068044C /* DefaultAnimatedVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 8D5E68CE20F2FA140068044C /* DefaultAnimatedVC.m */; };
8DA4304120BBE3D600C47917 /* SGPageContentCollectionView.m in Sources */ = {isa = PBXBuildFile; fileRef = 8DA4303F20BBE3D500C47917 /* SGPageContentCollectionView.m */; };
8DA4304420BBEDEC00C47917 /* UIButton+SGPagingView.m in Sources */ = {isa = PBXBuildFile; fileRef = 8DA4304320BBEDEC00C47917 /* UIButton+SGPagingView.m */; };
Expand Down Expand Up @@ -162,9 +162,10 @@
8D0F8E5E1FD95E2300F30426 /* ChildVCTwo.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = ChildVCTwo.xib; sourceTree = "<group>"; };
8D1A05F620B441B800743EAD /* DefaultAttributedTitleVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DefaultAttributedTitleVC.h; sourceTree = "<group>"; };
8D1A05F720B441B800743EAD /* DefaultAttributedTitleVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DefaultAttributedTitleVC.m; sourceTree = "<group>"; };
8D1D282221114426009B3DF4 /* ChhildTempPopGestureVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ChhildTempPopGestureVC.h; sourceTree = "<group>"; };
8D1D282321114426009B3DF4 /* ChhildTempPopGestureVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ChhildTempPopGestureVC.m; sourceTree = "<group>"; };
8D1D282421114426009B3DF4 /* ChhildTempPopGestureVC.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ChhildTempPopGestureVC.xib; sourceTree = "<group>"; };
8D5BB743213AC303000E929E /* ChildTempPopGestureVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ChildTempPopGestureVC.h; sourceTree = "<group>"; };
8D5BB744213AC303000E929E /* ChildTempPopGestureVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ChildTempPopGestureVC.m; sourceTree = "<group>"; };
8D5BB747213AC570000E929E /* SGPagingViewPopGestureVC.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SGPagingViewPopGestureVC.m; sourceTree = "<group>"; };
8D5BB748213AC570000E929E /* SGPagingViewPopGestureVC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SGPagingViewPopGestureVC.h; sourceTree = "<group>"; };
8D5E68CD20F2FA140068044C /* DefaultAnimatedVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DefaultAnimatedVC.h; sourceTree = "<group>"; };
8D5E68CE20F2FA140068044C /* DefaultAnimatedVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DefaultAnimatedVC.m; sourceTree = "<group>"; };
8DA4303F20BBE3D500C47917 /* SGPageContentCollectionView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SGPageContentCollectionView.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -306,6 +307,7 @@
182513E31F21D1600039F4A2 /* SGPageTitle */,
182513DE1F21D1600039F4A2 /* SGPageContent */,
182513DB1F21D1600039F4A2 /* Category */,
8D5BB746213AC532000E929E /* Controller */,
);
path = SGPagingView;
sourceTree = "<group>";
Expand Down Expand Up @@ -416,13 +418,21 @@
8D0F8E411FD95E2300F30426 /* ChildFourthPopGestureNextVC.h */,
8D0F8E421FD95E2300F30426 /* ChildFourthPopGestureNextVC.m */,
8D0F8E431FD95E2300F30426 /* ChildFourthPopGestureNextVC.xib */,
8D1D282221114426009B3DF4 /* ChhildTempPopGestureVC.h */,
8D1D282321114426009B3DF4 /* ChhildTempPopGestureVC.m */,
8D1D282421114426009B3DF4 /* ChhildTempPopGestureVC.xib */,
8D5BB743213AC303000E929E /* ChildTempPopGestureVC.h */,
8D5BB744213AC303000E929E /* ChildTempPopGestureVC.m */,
);
path = ChildVC;
sourceTree = "<group>";
};
8D5BB746213AC532000E929E /* Controller */ = {
isa = PBXGroup;
children = (
8D5BB748213AC570000E929E /* SGPagingViewPopGestureVC.h */,
8D5BB747213AC570000E929E /* SGPagingViewPopGestureVC.m */,
);
path = Controller;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -538,7 +548,6 @@
182513501F21CAC30039F4A2 /* Assets.xcassets in Resources */,
8D0F8E601FD95E2300F30426 /* ChildFirstPopGestureNextVC.xib in Resources */,
1825134E1F21CAC30039F4A2 /* Main.storyboard in Resources */,
8D1D282621114426009B3DF4 /* ChhildTempPopGestureVC.xib in Resources */,
8D0F8E731FD95E2300F30426 /* ChildVCTwo.xib in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -577,6 +586,7 @@
182513EA1F21D1600039F4A2 /* SGPageContentScrollView.m in Sources */,
8D0F8E611FD95E2300F30426 /* ChildFirstPopGestureVC.m in Sources */,
8D0F8E6E1FD95E2300F30426 /* ChildVCSeven.m in Sources */,
8D5BB745213AC303000E929E /* ChildTempPopGestureVC.m in Sources */,
8D0F8E371FD95DE300F30426 /* DefaultStaticVC.m in Sources */,
182513D21F21CC340039F4A2 /* NavigationBarVC.m in Sources */,
8DCB02AA1F95B0FF00C9598A /* SGPageTitleViewConfigure.m in Sources */,
Expand All @@ -585,13 +595,13 @@
8D0F8E341FD95DE300F30426 /* DefaultPopGestureVC.m in Sources */,
8D0F8E661FD95E2300F30426 /* ChildVCEight.m in Sources */,
8D0F8E6D1FD95E2300F30426 /* ChildVCOne.m in Sources */,
8D1D282521114426009B3DF4 /* ChhildTempPopGestureVC.m in Sources */,
8DA4304720BBEF3B00C47917 /* DefaultImageVC.m in Sources */,
8D0F8E671FD95E2300F30426 /* ChildVCFive.m in Sources */,
8D0F8E621FD95E2300F30426 /* ChildFourthPopGestureNextVC.m in Sources */,
8D0F8E6C1FD95E2300F30426 /* ChildVCNine.m in Sources */,
9A3EDE201FF4C5C600DC1EB2 /* DefaultDynamicVC.m in Sources */,
8D0F8E721FD95E2300F30426 /* ChildVCTwo.m in Sources */,
8D5BB749213AC570000E929E /* SGPagingViewPopGestureVC.m in Sources */,
8D0F8E6A1FD95E2300F30426 /* ChildVCFull.m in Sources */,
8DF628F720A94CBA00B7B85A /* DefaultSystemVC.m in Sources */,
182513481F21CAC30039F4A2 /* AppDelegate.m in Sources */,
Expand Down
57 changes: 0 additions & 57 deletions SGPagingViewExample/ChildVC/ChhildTempPopGestureVC.m

This file was deleted.

36 changes: 0 additions & 36 deletions SGPagingViewExample/ChildVC/ChhildTempPopGestureVC.xib

This file was deleted.

3 changes: 2 additions & 1 deletion SGPagingViewExample/ChildVC/ChildFirstPopGestureNextVC.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
//

#import <UIKit/UIKit.h>
#import "SGPagingViewPopGestureVC.h"

@interface ChildFirstPopGestureNextVC : UIViewController
@interface ChildFirstPopGestureNextVC : SGPagingViewPopGestureVC

@end
12 changes: 6 additions & 6 deletions SGPagingViewExample/ChildVC/ChildFirstPopGestureNextVC.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import "ChildFirstPopGestureNextVC.h"
#import "ChhildTempPopGestureVC.h"
#import "ChildTempPopGestureVC.h"

@interface ChildFirstPopGestureNextVC ()

Expand All @@ -21,6 +21,11 @@ - (void)viewDidLoad {
[self customLeftBarButtonItem];
}

- (IBAction)pushToNextVC:(id)sender {
ChildTempPopGestureVC *tempVC = [[ChildTempPopGestureVC alloc] init];
[self.navigationController pushViewController:tempVC animated:YES];
}

- (void)customLeftBarButtonItem {
UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
[button setTitle:@"back" forState:(UIControlStateNormal)];
Expand All @@ -34,11 +39,6 @@ - (void)popGesture {
[self.navigationController popViewControllerAnimated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
ChhildTempPopGestureVC *tempVC = [[ChhildTempPopGestureVC alloc] init];
[self.navigationController pushViewController:tempVC animated:YES];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
Expand Down
Loading

0 comments on commit f304f4c

Please sign in to comment.