版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2017.07.15 |
前言
在app中堰氓,我們經(jīng)常有需求就是滑動(dòng)左側(cè)邊距向右害捕,就會(huì)回到上一級(jí)控制器是目。感興趣的可以看看我寫的其他小技巧。
1. 實(shí)用小技巧(一):UIScrollView中上下左右滾動(dòng)方向的判斷
2. 實(shí)用小技巧(二):屏幕橫豎屏的判斷和相關(guān)邏輯
3.實(shí)用小技巧(三):點(diǎn)擊手勢(shì)屏蔽子視圖的響應(yīng)
4.實(shí)用小技巧(四):動(dòng)態(tài)的增刪標(biāo)簽視圖
5.實(shí)用小技巧(五):通過相冊(cè)或者相機(jī)更改圖標(biāo)
6.實(shí)用小技巧(六):打印ios里所有字體
7. 實(shí)用小技巧(七):UITableViewCell自適應(yīng)行高的計(jì)算
8. 實(shí)用小技巧(八):數(shù)字余額顯示的分隔
9.實(shí)用小技巧(九):類頭條模糊背景的實(shí)現(xiàn)
10.實(shí)用小技巧(十):晃動(dòng)手機(jī)換后臺(tái)服務(wù)器網(wǎng)絡(luò)
11.實(shí)用小技巧(十一):scrollView及其子類顯示的一些異常處理
12.實(shí)用小技巧(十二):頭像圖片縮放以及保存到相冊(cè)簡(jiǎn)單功能的實(shí)現(xiàn)
13.實(shí)用小技巧(十三):一種類酷我音樂盒動(dòng)畫實(shí)現(xiàn)
14.實(shí)用小技巧(十四):生成跳往applestore指定app的方法
功能需求
很多時(shí)候我們的app為了獲得很好的交互體驗(yàn)石窑,那么只要不是跟控制器,就可以向右滑動(dòng)屏幕蚓炬,返回上一級(jí)控制器松逊。其實(shí)自從iOS7以后系統(tǒng)默認(rèn)自帶了側(cè)滑功能,當(dāng)用戶在界面的左邊滑動(dòng)的時(shí)候肯夏,就會(huì)有側(cè)滑功能经宏,返回上一級(jí)控制器。
首先要讓大家看一下代碼組織結(jié)構(gòu)驯击。
下面看一下代碼烁兰。
1.AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
JJVC1 *vc1 = [[JJVC1 alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc1];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
2. JJVC1.m
#import "JJVC1.h"
#import "JJVC2.h"
@interface JJVC1 ()
@end
@implementation JJVC1
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"VC1";
self.view.backgroundColor = [UIColor magentaColor];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewDidTapped)];
[self.view addGestureRecognizer:tapGesture];
}
#pragma mark - Action && Notification
- (void)viewDidTapped
{
JJVC2 *vc2 = [[JJVC2 alloc] init];
[self.navigationController pushViewController:vc2 animated:YES];
}
@end
3.JJVC2.m
#import "JJVC2.h"
#import "JJVC3.h"
@interface JJVC2 ()
@end
@implementation JJVC2
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"VC2";
self.view.backgroundColor = [UIColor blueColor];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewDidTapped)];
[self.view addGestureRecognizer:tapGesture];
}
#pragma mark - Action && Notification
- (void)viewDidTapped
{
JJVC3 *vc3 = [[JJVC3 alloc] init];
[self.navigationController pushViewController:vc3 animated:YES];
}
@end
4. JJVC3.m
#import "JJVC3.h"
@interface JJVC3 ()
@end
@implementation JJVC3
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"VC3";
self.view.backgroundColor = [UIColor greenColor];
}
@end
系統(tǒng)此時(shí)自帶的側(cè)滑效果是管用的,可以返回上一級(jí)控制器徊都,具體效果如下所示沪斟。
手勢(shì)失效
有時(shí)候我們會(huì)發(fā)現(xiàn)左滑的手勢(shì)失效了,具體失效的原因很多暇矫,其中有一種手動(dòng)的方式可以放左邊側(cè)滑的返回上一級(jí)控制器失效主之,我們?cè)趘c2和vc3中择吊,加入一句代碼。
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
就會(huì)發(fā)現(xiàn)左邊側(cè)滑功能失效了槽奕。手勢(shì)可以失效几睛,那我們有時(shí)候是別的原因引起的,如果手勢(shì)失效我們可以這么解決史翘。
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
但是這里還有個(gè)問題:這行代碼確實(shí)可以實(shí)現(xiàn)滑動(dòng)返回了枉长,但是在根視圖的時(shí)候也可以側(cè)滑返回,這樣當(dāng)你再次在根視圖觸發(fā)一些 Action 的時(shí)候就不執(zhí)行了琼讽。具體解決方法如下必峰。
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (self.navigationController.viewControllers.count > 1) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}
else{
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
}
還可以調(diào)用系統(tǒng)的其他方法完成這個(gè)功能,可以遵循下面的方法嘗試一下钻蹬。
- 獲取系統(tǒng)自帶滑動(dòng)手勢(shì)的target對(duì)象吼蚁。
id target = self.navigationController.interactivePopGestureRecognizer.delegate;
- 創(chuàng)建全屏滑動(dòng)手勢(shì)~調(diào)用系統(tǒng)自帶滑動(dòng)手勢(shì)的target的action方法。
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
- 設(shè)置手勢(shì)代理~攔截手勢(shì)觸發(fā)问欠。
pan.delegate = self;
- 給導(dǎo)航控制器的view添加全屏滑動(dòng)手勢(shì)肝匆。
[self.view addGestureRecognizer:pan];
- 將系統(tǒng)自帶的滑動(dòng)手勢(shì)禁用。
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
- 攔截手勢(shì)觸發(fā)的方法顺献。
// return NO;則不需要觸發(fā)滑動(dòng)手勢(shì)
// return YES;則需要觸發(fā)滑動(dòng)手勢(shì)
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{}
下面我們看一下代碼旗国。
#import "JJVC2.h"
#import "JJVC3.h"
@interface JJVC2 () <UIGestureRecognizerDelegate>
@end
@implementation JJVC2
#pragma mark - Override Base Function
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"VC2";
self.view.backgroundColor = [UIColor blueColor];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewDidTapped)];
[self.view addGestureRecognizer:tapGesture];
id target = self.navigationController.interactivePopGestureRecognizer.delegate;
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
pan.delegate = self;
[self.view addGestureRecognizer:pan];
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
#pragma mark - Action && Notification
- (void)viewDidTapped
{
JJVC3 *vc3 = [[JJVC3 alloc] init];
[self.navigationController pushViewController:vc3 animated:YES];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
//如果有多種手勢(shì)的話,可以在這里進(jìn)行自定義設(shè)置注整,我這里就選擇均有效了能曾。
return YES;
}
@end
具體效果如下所示。
這里看見我只在從vc2中加的代碼肿轨,所以可以左滑動(dòng)返回到vc1寿冕,但是沒有在vc3中加入這段代碼,vc3中系統(tǒng)的返回還被我禁掉了椒袍,所以只能點(diǎn)擊導(dǎo)航左側(cè)返回按鈕返回了驼唱。
后記
這些小功能,都很簡(jiǎn)單驹暑,玩玩還挺有意思~~~