iOS開(kāi)發(fā)-3D-touch簡(jiǎn)單實(shí)現(xiàn)

一:主屏幕按壓應(yīng)用圖標(biāo)展示快捷選項(xiàng)

注意:應(yīng)用最多有4個(gè)快捷選項(xiàng)標(biāo)簽。

1.靜態(tài)標(biāo)簽

? 1-打開(kāi)項(xiàng)目的plist文件,添加UIApplicationShortcutItems (類(lèi)型為Array).

? 2-為UIApplicationShortcutItems添加item (類(lèi)型為Dictionary).

? ? ? ? ? ?UIApplicationShortcutItems:數(shù)組中的元素就是我們的那些快捷選項(xiàng)標(biāo)簽。

? ? ? ? ? ?UIApplicationShortcutItemTitle:標(biāo)簽標(biāo)題(必填)

? ? ? ? ? ?UIApplicationShortcutItemType:標(biāo)簽的唯一標(biāo)識(shí)(必填)

? ? ? ? ? ?UIApplicationShortcutItemIconType:使用系統(tǒng)圖標(biāo)的類(lèi)型,如搜索、定位、home等 (可選)

? ? ? ? ? ? UIApplicationShortcutItemIconFile:使用項(xiàng)目中的圖片作為標(biāo)簽圖標(biāo)(可選)

? ? ? ? ? ? UIApplicationShortcutItemSubtitle:標(biāo)簽副標(biāo)題(可選)

? ? ? ? ? ? UIApplicationShortcutItemUserInfo:字典信息,如傳值使用(可選)


2.動(dòng)態(tài)標(biāo)簽

在AppDelegate.m中添加代碼

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中添加對(duì)應(yīng)的代碼

//創(chuàng)建應(yīng)用圖標(biāo)上的3D touch快捷選項(xiàng)

[self creatShortcutItem];

//創(chuàng)建應(yīng)用圖標(biāo)上的3D touch快捷選項(xiàng)

- (void)creatShortcutItem {

//創(chuàng)建系統(tǒng)風(fēng)格的icon

UIApplicationShortcutIcon *icon =[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];

//創(chuàng)建自定義圖標(biāo)的icon

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

//創(chuàng)建快捷選項(xiàng)

UIApplicationShortcutItem * item = [[UIApplicationShortcutItem alloc]initWithType:@"com.mycompany.myapp.share"localizedTitle:@"分享"localizedSubtitle:@"分享副標(biāo)題"icon:icon userInfo:nil];

//添加到快捷選項(xiàng)數(shù)組

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

}

/*需要跳轉(zhuǎn)到對(duì)應(yīng)界面的,就根據(jù)之前設(shè)置的快捷選項(xiàng)標(biāo)簽唯一標(biāo)識(shí),根據(jù)不同標(biāo)識(shí)執(zhí)行不同的操作*/



二、peek(展示預(yù)覽)和pop(跳頁(yè)至預(yù)覽的界面)

1.首先給view注冊(cè)3DTouch的peek(預(yù)覽)和pop功能,我這里給cell注冊(cè)3DTouch的peek(預(yù)覽)和pop功能

舉個(gè)簡(jiǎn)單例子

-(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注冊(cè)3DTouch的peek(預(yù)覽)和pop功能[self registerForPreviewingWithDelegate:self sourceView:cell];

}else{

NSLog(@"3D Touch 無(wú)效");

}returncell;

}

2.需要繼承協(xié)議UIViewControllerPreviewingDelegate

3.實(shí)現(xiàn)UIViewControllerPreviewingDelegate方法

//peek(預(yù)覽)

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

{

//獲取按壓的cell所在行,[previewingContext sourceView]就是按壓的那個(gè)視圖NSIndexPath *indexPath = [_myTableView indexPathForCell:(UITableViewCell*)[previewingContext sourceView]];

//設(shè)定預(yù)覽的界面

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

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

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

childVC.str= [NSString stringWithFormat:@"我是%@,用力按一下進(jìn)來(lái)",_myArray[indexPath.row]];

//調(diào)整不被虛化的范圍,按壓的那個(gè)cell不被虛化(輕輕按壓時(shí)周邊會(huì)被虛化,再少用力展示預(yù)覽,再加力跳頁(yè)至設(shè)定界面)

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

previewingContext.sourceRect=rect;//返回預(yù)覽界面returnchildVC;

}

//pop(按用點(diǎn)力進(jìn)入)

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

[self showViewController:viewControllerToCommit sender:self];

}

以上是就是3d-touch技術(shù)簡(jiǎn)單的實(shí)現(xiàn),之后有新的想法會(huì)不斷的補(bǔ)充。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容