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技术简单的实现,之后有新的想法会不断的补充。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 专著:http://www.jianshu.com/p/3443a3b27b2d 1.简单的介绍一下3D Touc...
    violafa阅读 1,040评论 1 0
  • 1.简单的介绍一下3D Touch 3D Touch的触控技术,被苹果称为新一代多点触控技术。其实,就是此前在Ap...
    Camille_chen阅读 12,102评论 19 33
  • 一、3D Touch 简介 1.什么是3D Touch? 3D Touch是一种立体触控技术,被苹果称为新一代多点...
    予独爱秋天的梅花阅读 801评论 0 0
  • 3D Touch简介 2015年,苹果发布了iOS9以及iphone6s/iphone6s Plus,其中最具有创...
    爱恨的潮汐阅读 390评论 0 2
  • 3D Touch简介 2015年,苹果发布了iOS9以及iphone6s/iphone6s Plus,其中最具有创...
    简简蜗牛阅读 632评论 0 0