iOS开发-3D-touch简单实现

一:主屏幕按压应用图标展示快捷选项

注意:应用最多有4个快捷选项标签。

1.静态标签

  1-打开项目的plist文件,添加UIApplicationShortcutItems (类型为Array).

  2-为UIApplicationShortcutItems添加item (类型为Dictionary).

           UIApplicationShortcutItems:数组中的元素就是我们的那些快捷选项标签。

           UIApplicationShortcutItemTitle:标签标题(必填)

           UIApplicationShortcutItemType:标签的唯一标识(必填)

           UIApplicationShortcutItemIconType:使用系统图标的类型,如搜索、定位、home等 (可选)

            UIApplicationShortcutItemIconFile:使用项目中的图片作为标签图标(可选)

            UIApplicationShortcutItemSubtitle:标签副标题(可选)

            UIApplicationShortcutItemUserInfo:字典信息,如传值使用(可选)


2.动态标签

在AppDelegate.m中添加代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中添加对应的代码

//创建应用图标上的3D touch快捷选项

[self creatShortcutItem];

//创建应用图标上的3D touch快捷选项

- (void)creatShortcutItem {

//创建系统风格的icon

UIApplicationShortcutIcon *icon =[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];

//创建自定义图标的icon

//UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"分享.png"];

//创建快捷选项

UIApplicationShortcutItem * item = [[UIApplicationShortcutItem alloc]initWithType:@"com.mycompany.myapp.share"localizedTitle:@"分享"localizedSubtitle:@"分享副标题"icon:icon userInfo:nil];

//添加到快捷选项数组

[UIApplication sharedApplication].shortcutItems =@[item];

}

/*需要跳转到对应界面的,就根据之前设置的快捷选项标签唯一标识,根据不同标识执行不同的操作*/



二、peek(展示预览)和pop(跳页至预览的界面)

1.首先给view注册3DTouch的peek(预览)和pop功能,我这里给cell注册3DTouch的peek(预览)和pop功能

举个简单例子

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"myCell"];if(cell ==nil) {

cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myCell"];

}

cell.textLabel.text=_myArray[indexPath.row];if(self.traitCollection.forceTouchCapability ==UIForceTouchCapabilityAvailable) {

NSLog(@"3D Touch  可用!");//给cell注册3DTouch的peek(预览)和pop功能[self registerForPreviewingWithDelegate:self sourceView:cell];

}else{

NSLog(@"3D Touch 无效");

}returncell;

}

2.需要继承协议UIViewControllerPreviewingDelegate

3.实现UIViewControllerPreviewingDelegate方法

//peek(预览)

-(nullable UIViewController *)previewingContext:(id)previewingContext viewControllerForLocation:(CGPoint)location

{

//获取按压的cell所在行,[previewingContext sourceView]就是按压的那个视图NSIndexPath *indexPath = [_myTableView indexPathForCell:(UITableViewCell*)[previewingContext sourceView]];

//设定预览的界面

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main"bundle:nil];

SearchViewController*childVC = [storyboard instantiateViewControllerWithIdentifier:@"searchController"];

childVC.preferredContentSize= CGSizeMake(0.0f,500.0f);

childVC.str= [NSString stringWithFormat:@"我是%@,用力按一下进来",_myArray[indexPath.row]];

//调整不被虚化的范围,按压的那个cell不被虚化(轻轻按压时周边会被虚化,再少用力展示预览,再加力跳页至设定界面)

CGRect rect = CGRectMake(0,0, self.view.frame.size.width,40);

previewingContext.sourceRect=rect;//返回预览界面returnchildVC;

}

//pop(按用点力进入)

-(void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {

[self showViewController:viewControllerToCommit sender:self];

}

以上是就是3d-touch技术简单的实现,之后有新的想法会不断的补充。

你可能感兴趣的:(iOS开发-3D-touch简单实现)