美文网首页
ios - 右滑返回手势和App间的跳转

ios - 右滑返回手势和App间的跳转

作者: fjytqiu | 来源:发表于2016-05-22 22:54 被阅读1020次

一、右滑返回手势:

1. 效果和系统的一样,用于解决leftNavigationItem自定义导致系统右滑手势失灵
步骤 :

1)设置代理为导航类(BaseNavigationController),并遵守协议UIGestureRecognizerDelegate
2)实现-gestureRecognizerShouldBegin:代理方法。
🍐(例子)

@implementation UINavigationController
- (void)viewDidLoad {
  [super viewDidLoad];
  self.interactivePopGestureRecognizer.delegate = self;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
// 注意:只有非根控制器才有滑动返回功能,根控制器没有。
    // 判断导航控制器是否只有一个子控制器,如果只有一个子控制器,肯定是根控制器
    return self.childViewControllers.count > 1;
}
2.全屏右滑返回手势

1).系统自带的手势是UIScreenEdgePanGestureRecognizer类型对象,屏幕边缘滑动手势
2).系统自带手势target是_UINavigationInteractiveTransition类型的对象
3).target调用的action方法名叫handleNavigationTransition:

注意点:

1.禁止系统自带滑动手势使用。
2.只有导航控制器的非根控制器才需要触发手势,使用手势代理,控制手势触发。
🍐(例子)

- (void)viewDidLoad {
    [super viewDidLoad];
    // 获取系统自带滑动手势的target对象
    id target = self.interactivePopGestureRecognizer.delegate;
    // 创建全屏滑动手势,调用系统自带滑动手势的target的action方法
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    // 设置手势代理,拦截手势触发
    pan.delegate = self;
    // 给导航控制器的view添加全屏滑动手势
    [self.view addGestureRecognizer:pan];
    // 禁止使用系统自带的滑动手势
    self.interactivePopGestureRecognizer.enabled = NO;
}
// 什么时候调用:每次触发手势之前都会询问下代理,是否触发。
// 作用:拦截手势触发
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return self.childViewControllers.count > 1;
}

二、App间的相互跳转(在应用中打开外部应用)

先假定在下文中,正在运行的应用:openApp; 被打开的应用为:openedApp

  1. 在openedApp的info中设置urlschmes(可自己根据规范设置,下图为简便直接设置为“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://"]];
    }
}

以上就是简单的实现App间的相互跳转的步骤

以下补充截取自:

http://blog.csdn.net/wangqiuyun/article/details/8081974

自定义处理URL,除了启动还需向外部应用发送参数时可以通过此来实现
testHello://
testHello://com.fcplayer.testHello
testHello://config=1&abar=2
这时我们在被启动应用中就必须进行自定义处理,在delegate中实现该消息(Cocos2d加在AppDelegate中),例如:

  • (BOOL)application:(UIApplication )applicationhandleOpenURL:(NSURL)url { // Do something withthe url here }

通常,我们会从参数中解析出URL以便在视图中显示或者存储到UserPreference。下面的例子把URL存储为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;  
}  

其他
基本上至此我们就已经实现一个应用程序中启动另外一个应用的功能,但是为了是我们的代码更加强壮,我在网上又找了一段访问代码,如下:

// 检查用户是否配置了AppId  
// 有没有准确配置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];  
    }  
}  

相关文章

网友评论

      本文标题:ios - 右滑返回手势和App间的跳转

      本文链接:https://www.haomeiwen.com/subject/cdewrttx.html