3D touch — ios9新特新开发

来源:互联网 发布:淘宝客采集软件免费版 编辑:程序博客网 时间:2024/05/01 23:29

iOS9提供了两种屏幕标签,分别是静态标签和动态标签(Home Screen Quick Actions)。
这里写图片描述

静态标签

打开我们项目的plist文件,添加如下项(选择框中并没有,需要我们手工敲上去)
这里写图片描述
图片资源为透明底图

先添加了一个UIApplicationShortcutItems的数组,这个数组中添加的元素就是对应的静态标签,在每个标签中我们需要添加一些设置的键值:

必填项—-

UIApplicationShortcutItemType 这个键值设置一个快捷通道类型的字符串

UIApplicationShortcutItemTitle 这个键值设置标签的标题

选填项—-

UIApplicationShortcutItemSubtitle 设置标签的副标题

UIApplicationShortcutItemIconType 设置标签Icon类型

UIApplicationShortcutItemIconFile 设置标签的Icon文件

UIApplicationShortcutItemUserInfo 设置信息字典(用于传值)

动态标签
AppDelegate.m文件中加如下

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    // 1.创建UIWindow    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[TTTableViewController alloc] init]];    self.window.rootViewController = nav;    //动态添加快捷启动    UIApplicationShortcutIcon * icon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"cash_icon"];    UIApplicationShortcutItem *item = [[UIApplicationShortcutItem alloc]initWithType:@"shortcutTypeTwo" localizedTitle:@"优惠券" localizedSubtitle:@"我的优惠券" icon:icon userInfo:nil];    [[UIApplication sharedApplication]setShortcutItems:@[item]];    // 让UIWindow显示出来(让窗口成为主窗口 并且显示出来)    // 一个应用程序只能有一个主窗口    [self.window makeKeyAndVisible];    return YES;}

接受标示跳转事件

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {    UINavigationController *nav = (UINavigationController *)self.window.rootViewController;   TTDetailViewController *detailVC = [[TTDetailViewController alloc] init];    if ([shortcutItem.type isEqualToString:@"shortcutTypeOne"]) {        detailVC.navTitle = @"one";    } else if ([shortcutItem.type isEqualToString:@"shortcutTypeTwo"]) {        detailVC.navTitle = @"two";    }    [nav pushViewController:detailVC animated:YES];}

在需要添加3Dtouch 的页面 ,判断是否可用添加

if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {        NSLog(@"3D Touch  可用!");        [self registerForPreviewingWithDelegate:self sourceView:self.view];    } else {        NSLog(@"3D Touch 无效");    }

遵循并实现UIViewControllerPreviewingDelegate

#pragma mark - UIViewControllerPreviewingDelegate- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {    self.selectedCell = [self searchCellWithPoint:location];    previewingContext.sourceRect = self.selectedCell.frame;    TTDetailViewController *detailVC = [[TTDetailViewController alloc] init];    detailVC.delegate = self;    detailVC.navTitle = self.selectedCell.textLabel.text;    return detailVC;}- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {    [self tableView:self.tableView didSelectRowAtIndexPath:[self.tableView indexPathForCell:self.selectedCell]];}// 根据一个点寻找对应cell并返回cell- (UITableViewCell *)searchCellWithPoint:(CGPoint)point {    UITableViewCell *cell = nil;    for (UIView *view in self.tableView.subviews) {        NSString *class = [NSString stringWithFormat:@"%@",view.class];        if (![class isEqualToString:@"UITableViewWrapperView"]) continue;        for (UIView *tempView in view.subviews) {            if ([tempView isKindOfClass:[UITableViewCell class]] && CGRectContainsPoint(tempView.frame, point)) {                cell = (UITableViewCell *)tempView;                break;            }        }        break;    }    return cell;}   

打开预览的视图的.m文件,TTDetailViewController.m 文件中处理

- (NSArray<id<UIPreviewActionItem>> *)previewActionItems {    //    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"删除" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {        if ([self.delegate respondsToSelector:@selector(detailViewController:DidSelectedDeleteItem:)]) {            NSLog(@"删除");        }    }];    //    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"返回" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {        if ([self.delegate respondsToSelector:@selector(detailViewControllerDidSelectedBackItem:)]) {            NSLog(@"返回");        }    }];    NSArray *actions = @[action1,action2];    return actions;}
0 0
原创粉丝点击