iOS 3DTouch功能

3DTouch目前在手機(jī)上有兩種體現(xiàn)方式,一種是用力按下app的圖片icon,會彈出選項(xiàng)菜單,就像電腦上的右鍵。一種是在應(yīng)用內(nèi)的界面上用力按下,彈出的預(yù)覽界面。

一、iocn按下效果

這里有兩種方式實(shí)現(xiàn):

1、通過plist文件靜態(tài)設(shè)置

Info.plist文件設(shè)置

UIApplicationShortcutItemUserInfo信息

UIApplicationShortcutItemIconFile圖標(biāo)名稱

UIApplicationShortcutItemIconType圖標(biāo)類型

UIApplicationShortcutItemTitle標(biāo)題

UIApplicationShortcutItemSubTitle副標(biāo)題

然后在AppDelegate里面實(shí)現(xiàn)代理方法,通過綁定的標(biāo)簽type來實(shí)現(xiàn)具體代碼。

-(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=@"第一個(gè)";

vc.view.backgroundColor=?[UIColorredColor];

[navpushViewController:vcanimated:YES];

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

UIViewController*vc?=?[[UIViewControlleralloc]init];

vc.title=@"第二個(gè)";

vc.view.backgroundColor=?[UIColorgreenColor];

[navpushViewController:vcanimated:YES];

}

}

2、通過代碼動態(tài)創(chuàng)建

首先在AppDelegate的didFinishLaunchingWithOptions里面初始化

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

[selfsetup3DTouch:application];

returnYES;

}

- (void)setup3DTouch:(UIApplication*)application{
/**
type 該item 唯一標(biāo)識符
localizedTitle :標(biāo)題
localizedSubtitle:副標(biāo)題
icon:icon圖標(biāo) 可以使用系統(tǒng)類型 也可以使用自定義的圖片
userInfo:用戶信息字典 自定義參數(shù),完成具體功能需求
*/
//? ? UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"標(biāo)簽.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圖標(biāo) */
application.shortcutItems = @[cameraItem,shareItem];
}
//最后在代理方法里面實(shí)現(xiàn)具體需求代碼
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
if([shortcutItem.type isEqualToString:@"ONE"]){
NSLog(@"第一個(gè)");
}else if ([shortcutItem.type isEqualToString:@"TWO"]){
NSLog(@"第二個(gè)");
}
}

二、應(yīng)用內(nèi)UI界面用力按下產(chǎn)生的3DTouch效果(這種方式也有兩種實(shí)現(xiàn)效果)

比如用力按下某個(gè)cell,彈出預(yù)覽的小視圖,同時(shí)上滑底部出現(xiàn)若干個(gè)選項(xiàng)(Peek功能)。

首先注冊需要實(shí)現(xiàn)Touch效果的View,判斷下設(shè)備系統(tǒng)支不支持,不然會崩潰

- (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;

}

把當(dāng)前的cell注冊綁定,然后試圖界面實(shí)現(xiàn)UIViewControllerPreviewingDelegate代理

#pragma mark - UIViewControllerPreviewingDelegate

//Peek代理方法

//1、彈出預(yù)覽效果

-(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個(gè)效果的基礎(chǔ)上,再繼續(xù)用力按下去,就會push預(yù)覽界面了。(pop功能)-(void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{

[self showViewController:viewControllerToCommit sender:self];

}

如果實(shí)現(xiàn)下面有幾個(gè)選項(xiàng)的功能,需要在你預(yù)覽界面里面(彈出的那個(gè)界面)實(shí)現(xiàn)一個(gè)方法,

- (NSArray> *)previewActionItems{

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

//添加點(diǎn)擊處理操作

}];

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];

//? ? //該按鈕可以是一個(gè)組,點(diǎn)擊該組時(shí),跳到組里面的按鈕.

//? ? UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:@"按鈕數(shù)組/剩下的按鈕" style:UIPreviewActionStyleDefault actions:@[action2, action3]];

//直接返回?cái)?shù)組.

//? ? 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,有的不需要,此時(shí)注冊綁定cell會出現(xiàn)問題。解決方法是:在當(dāng)前的ViewController里定義一個(gè)全局的字典,key是cell,value是context,每次先判斷這個(gè)cell有沒有添加過,有的話取消,然后在判斷需不需要添加。因?yàn)槿∠臅r(shí)候不是傳的cell,需要傳id<UIViewControllerPreviewing>這個(gè),就是注冊時(shí)候的返回值,所以要用字典把cell和id<UIViewControllerPreviewing>這個(gè)綁定起來。

取消注冊綁定
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容