iOS 3DTouch功能

3DTouch目前在手机上有两种体现方式,一种是用力按下app的图片icon,会弹出选项菜单,就像电脑上的右键。一种是在应用内的界面上用力按下,弹出的预览界面。

一、iocn按下效果

这里有两种方式实现:

1、通过plist文件静态设置

iOS 3DTouch功能_第1张图片
Info.plist文件设置

UIApplicationShortcutItemUserInfo信息

UIApplicationShortcutItemIconFile图标名称

UIApplicationShortcutItemIconType图标类型

UIApplicationShortcutItemTitle标题

UIApplicationShortcutItemSubTitle副标题

然后在AppDelegate里面实现代理方法,通过绑定的标签type来实现具体代码。

-(void)application:(UIApplication*)applicationperformActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItemcompletionHandler:(void(^)(BOOL))completionHandler{

UINavigationController*nav = (UINavigationController*)self.window.rootViewController;

if([shortcutItem.typeisEqualToString:@"ONE"]){

UIViewController*vc = [[UIViewControlleralloc]init];

vc.title=@"第一个";

vc.view.backgroundColor= [UIColorredColor];

[navpushViewController:vcanimated:YES];

}elseif([shortcutItem.typeisEqualToString:@"TWO"]){

UIViewController*vc = [[UIViewControlleralloc]init];

vc.title=@"第二个";

vc.view.backgroundColor= [UIColorgreenColor];

[navpushViewController:vcanimated:YES];

}

}

2、通过代码动态创建

首先在AppDelegate的didFinishLaunchingWithOptions里面初始化

- (BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

[selfsetup3DTouch:application];

returnYES;

}

- (void)setup3DTouch:(UIApplication*)application{
/**
type 该item 唯一标识符
localizedTitle :标题
localizedSubtitle:副标题
icon:icon图标 可以使用系统类型 也可以使用自定义的图片
userInfo:用户信息字典 自定义参数,完成具体功能需求
*/
//    UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"标签.png"];
UIApplicationShortcutIcon *cameraIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCompose];
UIApplicationShortcutItem *cameraItem = [[UIApplicationShortcutItem alloc]initWithType:@"ONE" localizedTitle:@"拍照" localizedSubtitle:@"" icon:cameraIcon userInfo:nil];
UIApplicationShortcutIcon *shareIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
UIApplicationShortcutItem *shareItem = [[UIApplicationShortcutItem alloc] initWithType:@"TWO" localizedTitle:@"分享" localizedSubtitle:@"" icon:shareIcon userInfo:nil];
/** 将items 添加到app图标 */
application.shortcutItems = @[cameraItem,shareItem];
}
//最后在代理方法里面实现具体需求代码
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
if([shortcutItem.type isEqualToString:@"ONE"]){
NSLog(@"第一个");
}else if ([shortcutItem.type isEqualToString:@"TWO"]){
NSLog(@"第二个");
}
}

二、应用内UI界面用力按下产生的3DTouch效果(这种方式也有两种实现效果)

比如用力按下某个cell,弹出预览的小视图,同时上滑底部出现若干个选项(Peek功能)。

首先注册需要实现Touch效果的View,判断下设备系统支不支持,不然会崩溃

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

static NSString *identifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (!cell) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

}

cell.selectionStyle = UITableViewCellSelectionStyleNone;

cell.textLabel.text = self.dataArray[indexPath.row];

if ([self respondsToSelector:@selector(traitCollection)]) {

if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)]) {

if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {

[self registerForPreviewingWithDelegate:self sourceView:cell];

}}}

return cell;

}

把当前的cell注册绑定,然后试图界面实现UIViewControllerPreviewingDelegate代理

#pragma mark - UIViewControllerPreviewingDelegate

//Peek代理方法

//1、弹出预览效果

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

NSIndexPath *index = [self.tableView indexPathForCell:(UITableViewCell *)[previewingContext sourceView]];   

NSString *title = self.dataArray[index.row];       

Touch3DShowViewController *showVC = [[Touch3DShowViewController alloc]init];   

showVC.string = title;   

CGRect rect = CGRectMake(0, 0,  previewingContext.sourceView.frame.size.width, previewingContext.sourceView.frame.size.height);   

previewingContext.sourceRect = rect;       

return showVC;

}

//pop代理方法

//2、在第1个效果的基础上,再继续用力按下去,就会push预览界面了。(pop功能)-(void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{

[self showViewController:viewControllerToCommit sender:self];

}

如果实现下面有几个选项的功能,需要在你预览界面里面(弹出的那个界面)实现一个方法,

- (NSArray> *)previewActionItems{

UIPreviewAction *action0 = [UIPreviewAction actionWithTitle:@"0普通的按钮" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

//添加点击处理操作

}];

UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"1取消" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

}];

UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"2选中" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

}];

UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"3选中" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {             

}];

return  @[action0, action1, action2, action3];

//    //该按钮可以是一个组,点击该组时,跳到组里面的按钮.

//    UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:@"按钮数组/剩下的按钮" style:UIPreviewActionStyleDefault actions:@[action2, action3]];

//直接返回数组.

//    return  @[action0,action1,actionGroup];

}

如果在别的控件上添加3DTouch,则需要在此控件上注册,剩下的不变。栗子:button添加3DTouch:

UIButton *buton = [UIButton buttonWithType:UIButtonTypeCustom];

buton.frame = CGRectMake(50, 50, 200, 30);

buton.backgroundColor = [UIColor redColor];

[buton setTitle:@"3DTouch测试" forState:UIControlStateNormal];

[buton addTarget:self action:@selector(addTimerView) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:buton];

if ([self respondsToSelector:@selector(traitCollection)]) {

if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)]) {

if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {

[self registerForPreviewingWithDelegate:self sourceView:buton];

}}}


bug记录:

如果cell是多样式的,有的cell需要3DTouch,有的不需要,此时注册绑定cell会出现问题。解决方法是:在当前的ViewController里定义一个全局的字典,key是cell,value是context,每次先判断这个cell有没有添加过,有的话取消,然后在判断需不需要添加。因为取消的时候不是传的cell,需要传id这个,就是注册时候的返回值,所以要用字典把cell和id这个绑定起来。

iOS 3DTouch功能_第2张图片
取消注册绑定

你可能感兴趣的:(iOS 3DTouch功能)