ios - 右滑返回手勢(shì)和App間的跳轉(zhuǎn)

一、右滑返回手勢(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

  1. 在openedApp的info中設(shè)置urlschmes(可自己根據(jù)規(guī)范設(shè)置,下圖為簡(jiǎn)便直接設(shè)置為“openedApp”)


  2. 在openApp中的info中 LSApplicationQueriesSchemes下添加nstring - url schemes


  3. 通過[[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];  
    }  
}  
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,380評(píng)論 4 61
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,063評(píng)論 25 709
  • 我又忍不住話嘮來了。 剛才在空間看到一個(gè)高中同學(xué)馬某某發(fā)的祝高考的說說,突然就想起他的高考經(jīng)歷。高一進(jìn)班他班里倒...
    沉伏閱讀 175評(píng)論 0 0
  • 今天男朋友接我下班,心情非常開心。因?yàn)楣ぷ饕睬『米鐾?,自己也讀書了,這一天過得很有價(jià)值。 本來開開心心的和男朋友去...
    靜靜diary閱讀 324評(píng)論 0 2
  • 上證指數(shù)3430.46+0.59% 深證指數(shù)11560.80-0.36% 中小板指8034.08-0.61% 創(chuàng)業(yè)...
    百股精看盤閱讀 330評(píng)論 0 0

友情鏈接更多精彩內(nèi)容