iOS 3DTouch功能

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

一、iocn按下效果

这里有两种方式实现:

1、通过plist文件静态设置

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<UIViewControllerPreviewing>这个,就是注册时候的返回值,所以要用字典把cell和id<UIViewControllerPreviewing>这个绑定起来。

取消注册绑定
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,302评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,232评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,337评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,977评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,920评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,194评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,638评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,319评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,455评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,379评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,426评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,106评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,696评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,786评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,996评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,467评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,043评论 2 341

推荐阅读更多精彩内容

  • 一.3DTouch的主要作用: 0.demo地址在最下面 1.Home Screen Quick Actions ...
    哆啦_阅读 2,239评论 6 5
  • 3D Touch,苹果iPhone 6s的新功能 有Peek Pop 两种新手势 实现点击app出现小弹框界面的方...
    静静的coding阅读 398评论 0 0
  • 这货其实很简单,我就简单做个用法的解析。 首先,你要使用的话,你得在info.plist里面加入部分键值对,如下:...
    晓龙歌阅读 613评论 0 2
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,467评论 1 14
  • 1、设置UILabel行间距 NSMutableAttributedString* attrString = [[...
    FF_911阅读 1,351评论 0 3