3-DTouch_IOS
對集成3DTouch的簡單講解
Demo下載地址:https://github.com/ShaoGitHub/3-DTouch_IOS
自從iPhone6s推出以后驼抹,其最大的亮點(diǎn)無疑就是最新攜帶的3DTouch功能。這項(xiàng)技術(shù)不僅給各個應(yīng)用增加更多方便快捷的入口赡麦,其產(chǎn)品本身的交互體驗(yàn)也是超級酷炫霜大,而且最重要的姻成,蘋果不僅推出該功能拓提,還開放其API的調(diào)用接口等限,這對廣大iOS開發(fā)者來說真是福音啊屋彪,不像之前iPhone5s推出的指紋解鎖功能鹉胖,要隔段好久才開放API接口握玛,可見蘋果對這項(xiàng)新功能的推廣之心之迫切够傍。閑話少說,本文就iOS開發(fā)中如何集成3DTouch做下簡單的講解挠铲。
開發(fā)環(huán)境及調(diào)試設(shè)備:
Xcode7或以上冕屯,iOS9或以上,iPhone6s或以上
3DTouch功能主要分為兩大塊:主屏幕Icon上的快捷標(biāo)簽(Home Screen Quick Actions)拂苹; Peek(預(yù)覽)和Pop(跳至預(yù)覽的詳細(xì)界面)
Home Screen Quick Actions的實(shí)現(xiàn)
主屏幕icon上的快捷標(biāo)簽的實(shí)現(xiàn)方式有兩種安聘,一種是在工程文件info.plist里靜態(tài)設(shè)置,另一種是代碼的動態(tài)實(shí)現(xiàn).
靜態(tài)設(shè)置
靜態(tài)設(shè)置方式如下圖所示:
下面是各個標(biāo)簽類型的說明瓢棒,plist文件里還沒提供UIApplicationShortcutItems選項(xiàng)浴韭,沒辦法,只能手動敲了脯宿,或者直接復(fù)制粘貼過去念颈。
UIApplicationShortcutItems:數(shù)組中的元素就是我們的那些快捷選項(xiàng)標(biāo)簽。
UIApplicationShortcutItemTitle:標(biāo)簽標(biāo)題(必填)
UIApplicationShortcutItemType:標(biāo)簽的唯一標(biāo)識 (必填)
UIApplicationShortcutItemIconType:使用系統(tǒng)圖標(biāo)的類型连霉,如搜索榴芳、定位、home等(可選)
UIApplicationShortcutItemIcon File:使用項(xiàng)目中的圖片作為標(biāo)簽圖標(biāo) (可選)
UIApplicationShortcutItemSubtitle:標(biāo)簽副標(biāo)題 (可選)
UIApplicationShortcutItemUserInfo:字典信息跺撼,如傳值使用 (可選)
動態(tài)實(shí)現(xiàn)
動態(tài)設(shè)置方式如下所示:</br>
//動態(tài)創(chuàng)建應(yīng)用圖標(biāo)上的3D touch快捷選項(xiàng)
- (void)creatShortcutItem
{
UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"com.test.shequ" localizedTitle:@"社區(qū)" localizedSubtitle:@"開啟心聲,展望全新世界" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeHome] userInfo:@{}];
[UIApplication sharedApplication].shortcutItems = @[item1];
}
到此窟感,主屏幕icon上的快捷標(biāo)簽創(chuàng)建就介紹完了,而他們點(diǎn)擊進(jìn)入頁面的實(shí)現(xiàn)就有點(diǎn)類似消息通知的實(shí)現(xiàn)方式了歉井,只要增加兩處代碼就好:首次啟動APP和APP沒被殺死從后臺啟動柿祈。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
//動態(tài)創(chuàng)建應(yīng)用圖標(biāo)上的3D touch快捷選項(xiàng)
[self creatShortcutItem];
UIApplicationShortcutItem *shortcutItem = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];
if (shortcutItem) {
//判斷設(shè)置的快捷選項(xiàng)標(biāo)簽唯一標(biāo)識,根據(jù)不同標(biāo)識執(zhí)行不同操作
if([shortcutItem.type isEqualToString:@"com.test.shengyou"]){
NSLog(@"新啟動APP-- 聲優(yōu)界面");
HallViewController *sayuView = [[HallViewController alloc] init];
[[self topViewController].navigationController pushViewController:sayuView animated:YES];
} else if ([shortcutItem.type isEqualToString:@"com.test.shequ"]) {
//進(jìn)入搜索界面
NSLog(@"新啟動APP-- 社區(qū)界面");
}
return NO;
}
return YES;
}
- (void)application:(UIApplication *)application performActionForShortcutItem:(nonnull UIApplicationShortcutItem *)shortcutItem completionHandler:(nonnull void (^)(BOOL))completionHandler
{
NSLog(@"%@",shortcutItem.userInfo);
if (shortcutItem) {
//判斷設(shè)置的快捷選項(xiàng)標(biāo)簽唯一標(biāo)識哩至,根據(jù)不同標(biāo)識執(zhí)行不同操作
if([shortcutItem.type isEqualToString:@"com.xys.shengyou"]){
NSLog(@"新啟動APP-- 聲優(yōu)界面");
HallViewController *sayuView = [[HallViewController alloc] init];
[[self topViewController].navigationController pushViewController:sayuView animated:YES];
} else if ([shortcutItem.type isEqualToString:@"com.xys.shequ"]) {
//進(jìn)入搜索界面
NSLog(@"新啟動APP-- 社區(qū)界面");
}
}
if (completionHandler) {
completionHandler(YES);
}
}
Peek和Pop的實(shí)現(xiàn)-四部曲
- 注冊(在哪個頁面上使用該功能就注冊在哪個頁面上)
[self registerForPreviewingWithDelegate:self sourceView:self.view];
-
繼承協(xié)議UIViewControllerPreviewingDelegate
@interface HallViewController ()<UIViewControllerPreviewingDelegate>
-
實(shí)現(xiàn)UIViewControllerPreviewingDelegate方法
//peek(預(yù)覽) - (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location { //獲取按壓的cell所在行谍夭,[previewingContext sourceView]就是按壓的那個視圖 //NSIndexPath *indexPath = [_myTableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]]; //設(shè)定預(yù)覽的界面 MyPreviewingViewController *childVC = [[MyPreviewingViewController alloc] init]; childVC.preferredContentSize = CGSizeMake(0.0f,500.0f); childVC.myStr = [NSString stringWithFormat:@"我是%@,用力按一下進(jìn)來-------",@"白紙上涂鴉"]; //調(diào)整不被虛化的范圍,按壓的那個cell不被虛化(輕輕按壓時周邊會被虛化憨募,再少用力展示預(yù)覽,再加力跳頁至設(shè)定界面) //CGRect rect = CGRectMake(0, 0, self.view.frame.size.width,40); CGRect rect = self.view.frame; previewingContext.sourceRect = rect; //返回預(yù)覽界面 return childVC; } //pop(按用點(diǎn)力進(jìn)入) - (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit { // [self.view addSubview:viewControllerToCommit.view]; [self showViewController:viewControllerToCommit sender:self]; }
-
當(dāng)彈出預(yù)覽時袁辈,上滑預(yù)覽視圖菜谣,出現(xiàn)預(yù)覽視圖中快捷選項(xiàng)
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems { // setup a list of preview actions UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"刪除" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"你點(diǎn)了-刪除" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil]; // [alert show]; }]; UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"置頂" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"你點(diǎn)了-置頂" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil]; // [alert show]; }]; UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"啥也不干" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) { // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"真的啥也不干?" message:nil delegate:nil cancelButtonTitle:@"確定" otherButtonTitles: nil]; // [alert show]; }]; NSArray *actions = @[action1,action2,action3]; // and return them (return the array of actions instead to see all items ungrouped) return actions; }
到此晚缩,3DTouch在APP中的集成就先介紹這些尾膊,3DTouch中還有個重要的屬性--壓力屬性(force 和 maximumPossibleForce)這里就不詳細(xì)介紹了,感興趣的同學(xué)可以去看下官方文檔荞彼,網(wǎng)上也很多相關(guān)資料冈敛。以上有說的不對的地方,還望高手指正鸣皂,相互學(xué)習(xí)抓谴,共同進(jìn)步.
Demo下載地址:https://github.com/ShaoGitHub/3-DTouch_IOS
我的簡書地址:http://www.reibang.com/u/e0c475eb47e9