一:主屏幕按壓應用圖標展示快捷選項
注意:應用最多有4個快捷選項標簽。
1.靜態標簽
? 1-打開項目的plist文件,添加UIApplicationShortcutItems (類型為Array).
? 2-為UIApplicationShortcutItems添加item (類型為Dictionary).
? ? ? ? ? ?UIApplicationShortcutItems:數組中的元素就是我們的那些快捷選項標簽。
? ? ? ? ? ?UIApplicationShortcutItemTitle:標簽標題(必填)
? ? ? ? ? ?UIApplicationShortcutItemType:標簽的唯一標識(必填)
? ? ? ? ? ?UIApplicationShortcutItemIconType:使用系統圖標的類型,如搜索、定位、home等 (可選)
? ? ? ? ? ? UIApplicationShortcutItemIconFile:使用項目中的圖片作為標簽圖標(可選)
? ? ? ? ? ? UIApplicationShortcutItemSubtitle:標簽副標題(可選)
? ? ? ? ? ? UIApplicationShortcutItemUserInfo:字典信息,如傳值使用(可選)
2.動態標簽
在AppDelegate.m中添加代碼
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中添加對應的代碼
//創建應用圖標上的3D touch快捷選項
[self creatShortcutItem];
//創建應用圖標上的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];
}
/*需要跳轉到對應界面的,就根據之前設置的快捷選項標簽唯一標識,根據不同標識執行不同的操作*/
二、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 無效");
}returncell;
}
2.需要繼承協議UIViewControllerPreviewingDelegate
3.實現UIViewControllerPreviewingDelegate方法
//peek(預覽)
-(nullable UIViewController *)previewingContext:(id)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;//返回預覽界面returnchildVC;
}
//pop(按用點力進入)
-(void)previewingContext:(id)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {
[self showViewController:viewControllerToCommit sender:self];
}
以上是就是3d-touch技術簡單的實現,之后有新的想法會不斷的補充。