一、右滑返回手勢(shì):
1. 效果和系統(tǒng)的一樣,用于解決leftNavigationItem自定義導(dǎo)致系統(tǒng)右滑手勢(shì)失靈
步驟 :
1)設(shè)置代理為導(dǎo)航類(BaseNavigationController),并遵守協(xié)議UIGestureRecognizerDelegate
2)實(shí)現(xiàn)-gestureRecognizerShouldBegin:代理方法。
??(例子)
@implementation UINavigationController
- (void)viewDidLoad {
[super viewDidLoad];
self.interactivePopGestureRecognizer.delegate = self;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
// 注意:只有非根控制器才有滑動(dòng)返回功能,根控制器沒有。
// 判斷導(dǎo)航控制器是否只有一個(gè)子控制器,如果只有一個(gè)子控制器,肯定是根控制器
return self.childViewControllers.count > 1;
}
2.全屏右滑返回手勢(shì)
1).系統(tǒng)自帶的手勢(shì)是UIScreenEdgePanGestureRecognizer類型對(duì)象,屏幕邊緣滑動(dòng)手勢(shì)
2).系統(tǒng)自帶手勢(shì)target是_UINavigationInteractiveTransition類型的對(duì)象
3).target調(diào)用的action方法名叫handleNavigationTransition:
注意點(diǎn):
1.禁止系統(tǒng)自帶滑動(dòng)手勢(shì)使用。
2.只有導(dǎo)航控制器的非根控制器才需要觸發(fā)手勢(shì),使用手勢(shì)代理,控制手勢(shì)觸發(fā)。
??(例子)
- (void)viewDidLoad {
[super viewDidLoad];
// 獲取系統(tǒng)自帶滑動(dòng)手勢(shì)的target對(duì)象
id target = self.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.interactivePopGestureRecognizer.enabled = NO;
}
// 什么時(shí)候調(diào)用:每次觸發(fā)手勢(shì)之前都會(huì)詢問下代理,是否觸發(fā)。
// 作用:攔截手勢(shì)觸發(fā)
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return self.childViewControllers.count > 1;
}
二、App間的相互跳轉(zhuǎn)(在應(yīng)用中打開外部應(yīng)用)
先假定在下文中,正在運(yùn)行的應(yīng)用:openApp; 被打開的應(yīng)用為:openedApp
-
在openedApp的info中設(shè)置urlschmes(可自己根據(jù)規(guī)范設(shè)置,下圖為簡(jiǎn)便直接設(shè)置為“openedApp”)
-
在openApp中的info中 LSApplicationQueriesSchemes下添加nstring - url schemes
通過[[UIApplication sharedApplication] openURL:]方法打開,
在打開之前先通過[application canOpenURL:url]判斷是否能打開(防止openedApp未安裝或版本過低等問題)
- (void)clickBtn {
UIApplication *application = [UIApplication sharedApplication];
NSURL *url = [NSURL URLWithString:@"com.openedApp://"];
if ([application canOpenURL:url]) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"打開openedApp" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
[alertView show];
}else {
// 前往 App Store 下載 openedApp
[application openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/openedApp/id0000?mt=8"]];
}
}
//pragma mark - UIAlertView delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"com.openedApp://"]];
}
}
以上就是簡(jiǎn)單的實(shí)現(xiàn)App間的相互跳轉(zhuǎn)的步驟
以下補(bǔ)充截取自:
http://blog.csdn.net/wangqiuyun/article/details/8081974
自定義處理URL,除了啟動(dòng)還需向外部應(yīng)用發(fā)送參數(shù)時(shí)可以通過此來實(shí)現(xiàn)
testHello://
testHello://com.fcplayer.testHello
testHello://config=1&abar=2
這時(shí)我們?cè)诒粏?dòng)應(yīng)用中就必須進(jìn)行自定義處理,在delegate中實(shí)現(xiàn)該消息(Cocos2d加在AppDelegate中),例如:
- (BOOL)application:(UIApplication )applicationhandleOpenURL:(NSURL)url { // Do something withthe url here }
通常,我們會(huì)從參數(shù)中解析出URL以便在視圖中顯示或者存儲(chǔ)到UserPreference。下面的例子把URL存儲(chǔ)為User Preference的url變量中或者打印出來
-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
if (!url) { return NO; }
NSString *URLString = [url absoluteString];
NSLog(@"%@",URLString);
//[[NSUserDefaults standardUserDefaults] setObject:URLString forKey:@"url"];
//[[NSUserDefaults standardUserDefaults] synchronize];
return YES;
}
其他
基本上至此我們就已經(jīng)實(shí)現(xiàn)一個(gè)應(yīng)用程序中啟動(dòng)另外一個(gè)應(yīng)用的功能,但是為了是我們的代碼更加強(qiáng)壯,我在網(wǎng)上又找了一段訪問代碼,如下:
// 檢查用戶是否配置了AppId
// 有沒有準(zhǔn)確配置Info的CFBundleURLSchemes字段
// 是不是可以正確打開
if (!kAppId) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Setup Error"
message:@"Missing app ID. You cannot run the app until you provide this in the code."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil,
nil];
[alertView show];
[alertView release];
} else {
// Now check that the URL scheme fb[app_id]://authorize is in the .plist and can
// be opened, doing a simple check without local app id factored in here
NSString *url = [NSString stringWithFormat:@"fb%@://authorize",kAppId];
BOOL bSchemeInPlist = NO; // find out if the sceme is in the plist file.
NSArray* aBundleURLTypes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];
if ([aBundleURLTypes isKindOfClass:[NSArray class]] &&
([aBundleURLTypes count] > 0)) {
NSDictionary* aBundleURLTypes0 = [aBundleURLTypes objectAtIndex:0];
if ([aBundleURLTypes0 isKindOfClass:[NSDictionary class]]) {
NSArray* aBundleURLSchemes = [aBundleURLTypes0 objectForKey:@"CFBundleURLSchemes"];
if ([aBundleURLSchemes isKindOfClass:[NSArray class]] &&
([aBundleURLSchemes count] > 0)) {
NSString *scheme = [aBundleURLSchemes objectAtIndex:0];
if ([scheme isKindOfClass:[NSString class]] &&
[url hasPrefix:scheme]) {
bSchemeInPlist = YES;
}
}
}
}
// Check if the authorization callback will work
BOOL bCanOpenUrl = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: url]];
if (!bSchemeInPlist || !bCanOpenUrl) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Setup Error"
message:@"Invalid or missing URL scheme. You cannot run the app until you set up a valid URL scheme in your .plist."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil,
nil];
[alertView show];
[alertView release];
}
}

