iOS9新特性 3DTouch 開發教程全解(含源碼)
本文主要講解3DTouch各種場景下的開發方法,開發主屏幕應用icon上的快捷選項標簽(Home Screen Quick Actions),靜態設置 UIApplicationShortcutItem ,動態添加、修改UIApplicationShortcutItem,peek和pop的實現。
一、3DTouch開發準備工作(讓模擬器也支持 3DTouch 的解決辦法)
需要支持3DTouch的設備,如iPhone6s或以上、iOS9或以上、Xcode7或以上,估計很多和我一樣的屌絲還沒有iPhone6s,別怕,github上有人為我們提供了這樣的一個插件,可以讓我們在模擬器上進行3D Touch的效果測試。 https://github.com/DeskConnect/SBShortcutMenuSimulator
安裝和使用git主頁里介紹的很清楚,只有一點需要注意,如果電腦中裝有Xcode6和Xcode7兩個版本,那個Xcode的編譯路徑,需要做如下修改。( Xcode2.app是你Xcode7版本的名字 )
sudo xcode-select -switch /Applications/Xcode2.app/Contents/Developer/
二、主屏幕 按壓應用圖標展示快捷選項 ( Home Screen Quick Actions )
應用最多有4個快捷選項標簽, iOS9為我們提供了2種方式來開發按壓應用圖標展示快捷選項功能(Home Screen Quick Actions)。
1.靜態標簽
打開我們項目的plist文件,添加如下項(選擇框中并沒有,需要我們手工敲上去)
UIApplicationShortcutItems:數組中的元素就是我們的那些快捷選項標簽。
UIApplicationShortcutItemTitle:標簽標題(必填)
UIApplicationShortcutItemType:標簽的唯一標識 (必填)
UIApplicationShortcutItemIconType:使用系統圖標的類型,如搜索、定位、home等(可選)
UIApplicationShortcutItemIcon File:使用項目中的圖片作為標簽圖標 (可選)
UIApplicationShortcutItemSubtitle:標簽副標題 (可選)
UIApplicationShortcutItemUserInfo:字典信息,如傳值使用 (可選)
2.動態標簽
在AppDelegate.m文件中加如下代碼:
- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; ViewController *mainView = [storyboard instantiateViewControllerWithIdentifier:@"mainController"]; UINavigationController *mainNav = [[UINavigationController alloc] initWithRootViewController:mainView]; self.window.rootViewController = mainNav; [self.window makeKeyAndVisible]; //創建應用圖標上的3D touch快捷選項 [self creatShortcutItem]; UIApplicationShortcutItem *shortcutItem = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey]; //如果是從快捷選項標簽啟動app,則根據不同標識執行不同操作,然后返回NO,防止調用- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler if (shortcutItem) { //判斷先前我們設置的快捷選項標簽唯一標識,根據不同標識執行不同操作 if([shortcutItem.type isEqualToString:@"com.mycompany.myapp.one"]){ NSArray *arr = @[@"hello 3D Touch"]; UIActivityViewController *vc = [[UIActivityViewController alloc]initWithActivityItems:arr applicationActivities:nil]; [self.window.rootViewController presentViewController:vc animated:YES completion:^{ }]; } else if ([shortcutItem.type isEqualToString:@"com.mycompany.myapp.search"]) {//進入搜索界面 SearchViewController *childVC = [storyboard instantiateViewControllerWithIdentifier:@"searchController"]; [mainNav pushViewController:childVC animated:NO]; } else if ([shortcutItem.type isEqualToString:@"com.mycompany.myapp.share"]) {//進入分享界面 SharedViewController *childVC = [storyboard instantiateViewControllerWithIdentifier:@"sharedController"]; [mainNav pushViewController:childVC animated:NO]; } return NO; } return YES;
}
//創建應用圖標上的3D touch快捷選項
- (void)creatShortcutItem { //創建系統風格的icon UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
// //創建自定義圖標的icon // UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"分享.png"];
//創建快捷選項
UIApplicationShortcutItem * item = [[UIApplicationShortcutItem alloc]initWithType:@"com.mycompany.myapp.share" localizedTitle:@"分享" localizedSubtitle:@"分享副標題" icon:icon userInfo:nil];
//添加到快捷選項數組
[UIApplication sharedApplication].shortcutItems = @[item];
}</pre>
效果圖:
3.點擊快捷選項標簽進入應用的響應
在AppDelegate.m文件中加如下代碼:
//如果app在后臺,通過快捷選項標簽進入app,則調用該方法,如果app不在后臺已殺死,則處理通過快捷選項標簽進入app的邏輯在- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions中
(void)application:(UIApplication )application performActionForShortcutItem:(UIApplicationShortcutItem )shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
UIStoryboard storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; ViewController mainView = [storyboard instantiateViewControllerWithIdentifier:@"mainController"]; UINavigationController *mainNav = [[UINavigationController alloc] initWithRootViewController:mainView]; self.window.rootViewController = mainNav; [self.window makeKeyAndVisible];
//判斷先前我們設置的快捷選項標簽唯一標識,根據不同標識執行不同操作 if([shortcutItem.type isEqualToString:@"com.mycompany.myapp.one"]){
NSArray *arr = @[@"hello 3D Touch"]; UIActivityViewController *vc = [[UIActivityViewController alloc]initWithActivityItems:arr applicationActivities:nil]; [self.window.rootViewController presentViewController:vc animated:YES completion:^{ }];
} else if ([shortcutItem.type isEqualToString:@"com.mycompany.myapp.search"]) {//進入搜索界面
SearchViewController *childVC = [storyboard instantiateViewControllerWithIdentifier:@"searchController"]; [mainNav pushViewController:childVC animated:NO];
} else if ([shortcutItem.type isEqualToString:@"com.mycompany.myapp.share"]) {//進入分享界面
SharedViewController *childVC = [storyboard instantiateViewControllerWithIdentifier:@"sharedController"]; [mainNav pushViewController:childVC animated:NO];
}
if (completionHandler) {
completionHandler(YES);
} }</pre>
4.修改UIApplicationShortcutItem
//獲取第0個shortcutItem UIApplicationShortcutItem *shortcutItem0 = [[UIApplication sharedApplication].shortcutItems objectAtIndex:0]; //將shortcutItem0的類型由UIApplicationShortcutItem改為可修改類型UIMutableApplicationShortcutItem UIMutableApplicationShortcutItem * newShortcutItem0 = [shortcutItem0 mutableCopy]; //修改shortcutItem的標題 [newShortcutItem0 setLocalizedTitle:@"按鈕1"]; //將shortcutItems數組改為可變數組 NSMutableArray *newShortcutItems = [[UIApplication sharedApplication].shortcutItems mutableCopy]; //替換原ShortcutItem [newShortcutItems replaceObjectAtIndex:0 withObject:newShortcutItem0]; [UIApplication sharedApplication].shortcutItems = newShortcutItems;
三、peek(展示預覽)和pop(跳頁至預覽的界面)
1. 首先給view注冊3DTouch的peek(預覽)和pop功能,我這里給cell注冊 3DTouch的peek(預覽)和pop功能
-(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注冊3DTouch的peek(預覽)和pop功能 [self registerForPreviewingWithDelegate:self sourceView:cell];
} else {
NSLog(@"3D Touch 無效");
} return cell; }</pre>
2.需要繼承協議UIViewControllerPreviewingDelegate
3.實現UIViewControllerPreviewingDelegate方法
//peek(預覽)
(nullable UIViewController )previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location { //獲取按壓的cell所在行,[previewingContext sourceView]就是按壓的那個視圖 NSIndexPath indexPath = [_myTableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];
//設定預覽的界面 UIStoryboard storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; SearchViewController childVC = [storyboard instantiateViewControllerWithIdentifier:@"searchController"]; childVC.preferredContentSize = CGSizeMake(0.0f,500.0f); childVC.str = [NSString stringWithFormat:@"我是%@,用力按一下進來",_myArray[indexPath.row]];
//調整不被虛化的范圍,按壓的那個cell不被虛化(輕輕按壓時周邊會被虛化,再少用力展示預覽,再加力跳頁至設定界面) CGRect rect = CGRectMake(0, 0, self.view.frame.size.width,40); previewingContext.sourceRect = rect;
//返回預覽界面 return childVC; }
//pop(按用點力進入)
(void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit { [self showViewController:viewControllerToCommit sender:self]; }</pre>
效果圖:( 當用戶按下時cell周邊會虛化,增加壓力達到一定值會彈出設定的預覽界面,繼續增加力按壓會跳頁至預覽界面 )
4.打開預覽的視圖的.m文件,我這里是 SearchViewController .m中加上如下代碼:
- (NSArray<id<UIPreviewActionItem>> )previewActionItems { // setup a list of preview actions UIPreviewAction action1 = [UIPreviewAction actionWithTitle:@"Aciton1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction _Nonnull action, UIViewController _Nonnull previewViewController) {
NSLog(@"Aciton1");
}];
UIPreviewAction action2 = [UIPreviewAction actionWithTitle:@"Aciton2" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Aciton2");
}];
UIPreviewAction action3 = [UIPreviewAction actionWithTitle:@"Aciton3" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"Aciton3");
}];
NSArray *actions = @[action1,action2,action3];
// and return them (return the array of actions instead to see all items ungrouped) return actions; }</pre>
效果圖:(當彈出預覽時,上滑預覽視圖,出現預覽視圖中快捷選項)
四、3DTouch壓力值的運用
直接上圖、上代碼更直觀,注釋也很清楚,這是我的SearchViewController界面。
直接在SearchViewController.m加這個方法即可,按壓SearchViewController中的任何視圖都會調用這個方法
//按住移動or壓力值改變時的回調 -(void)touchesMoved:(NSSet<UITouch > )touches withEvent:(UIEvent )event { NSArray arrayTouch = [touches allObjects]; UITouch touch = (UITouch )[arrayTouch lastObject]; //通過tag確定按壓的是哪個view,注意:如果按壓的是label,將label的userInteractionEnabled屬性設置為YES if (touch.view.tag == 105) {
NSLog(@"move壓力 = %f",touch.force); //紅色背景的label顯示壓力值 _lbForce.text = [NSString stringWithFormat:@"壓力%f",touch.force]; //紅色背景的label上移的高度=壓力值*100 _bottom.constant = ((UITouch *)[arrayTouch lastObject]).force * 100;
} }</pre>
好了,用不同力度按壓那個藍色背景的label,感受一下力度的變化吧,會看到隨著力度的變化紅色背景的label會上下移動。