3D Touch开发

 

在程序入口的代理方法里面执行下面代码???


//3D Touch 分为重压和轻压手势, 分别称作POP(第一段重压)和PEEK(第二段重压), 外面的图标只需要POP即可.

    //POP手势图标初始化
    //使用系统自带图标

    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
    UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
    //使用自己的图片
//    UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"自己的图片"];
    
    UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc]initWithType:@"item1" localizedTitle:@"标题1" localizedSubtitle:nil icon:icon1 userInfo:nil];
    UIApplicationShortcutItem *item2 = [[UIApplicationShortcutItem alloc]initWithType:@"item2" localizedTitle:@"标题2" localizedSubtitle:nil icon:icon2 userInfo:nil];
    
    NSArray *array = @[item1,item2];

    [UIApplication sharedApplication].shortcutItems = array;


点击图标之后会从这个入口进入App:

#pragma mark - 3DTouch触发的方法
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    //这里可以实现界面跳转等方法
    if ([shortcutItem.type isEqualToString:@"item1"]) {
        SFSafariViewController *sv = [[SFSafariViewController alloc]initWithURL:[NSURL URLWithString:@"http://weibo.com/p/1005055844400745/home?from=page_100505_profile&wvr=6&mod=data&is_all=1#place"]];
        _window.rootViewController = sv;
        NSLog(@"按压了第一个标题");
    }
    else if ([shortcutItem.type isEqualToString:@"item2"])
    {
        ViewController *vc = [[ViewController alloc]init];
        _window.rootViewController = vc;
        NSLog(@"按压了第二个标题");
    }
}

然后在处理逻辑!!!


PEEK(第二段重压),


//注册代理
    [self registerForPreviewingWithDelegate:self sourceView:self.view];

并遵守协议:UIViewControllerPreviewingDelegate



-(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    //固定这么写
    [self showViewController:viewControllerToCommit sender:self];
}

-(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
    //location就是重压点坐标,如果按压点在label上执行以下方法
    if (location.x > 100 && location.x < 200 && location.y > 100 && location.y < 200) {
        SFSafariViewController *mySV = [[SFSafariViewController alloc]initWithURL:[NSURL URLWithString:URL]];
        //第一次按压时,弹出的窗口尺寸,再次按压则跳转到mySV
        mySV.preferredContentSize = CGSizeMake(0, 400);
          previewingContext.sourceRect = _weiboLabel.frame;//设置高亮区域
        return mySV;
    }
    return nil;
}



你可能感兴趣的:(3D Touch开发)