1 3D Touch 的三大模塊
在我們的app中使用3D Touch功能,主要分一下三個模塊:
1.1 Home Screen Quick Actions
通過主屏幕的應用Icon,我們可以用3D Touch 呼出一個菜單,進行快速定位應用功能模塊相關功能的開發。
maps_directions_home_2x.png
1.2 Peek and Pop
這個功能是一套全新的用戶交互機制,在使用 3D Touch時, 會有如下三個交互階段:
(1) 輕按,提示用戶這里有3D Touch 的交互,會使交互空間周圍模糊
preview_available_2_2x.png
(2) 繼續深按,會出現預覽視圖
peek_2x.png
(3) 向上滑動預覽視圖通過視圖上的交互控件進行進一步交互
peek_quick_actions_2x.png
這個模塊的設計可以在網址鏈接上進行網頁的預覽交互。
1.3 Force Properties
iOS9 為我們提供了新的交互參數:force 和 maximumPossibleForce 。
2 Home Screen Quick Actions 功能
應用最多有4個快捷選項標簽,有兩種方式開發 Home Screen Quick Actions 功能。
2.1 靜態標簽
打開項目的plist文件,添加如下項
靜態標簽.png
選項介紹
UIApplicationShortcutItems : 數組中的元素就是我們的那些快捷選項標簽.
UIApplicationShortcutItemType : 標簽的唯一 標示(必填)
UIApplicationShortcutItemTitle : 標簽的標題(必填)
UIApplicationShortcutItemSubtitle : 標簽的副標題 (選填)
UIApplicationShortcutItemIconType : 標簽的Icon類型 (選填)
UIApplicationShortcutItemIconFile : 標簽的Icon 文件,圖標格式35x35像素單色 (選填)
UIApplicationShortcutItemUserInfo: 字典信息,傳值使用(選填)
2.2 動態標簽
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] init];
ViewController *mainView = [[ViewController alloc] init];
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.dimi.diandi86.main"]){
DimiViewController *dimiVC = [[DimiViewController alloc] init];
[self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
}];
} else if ([shortcutItem.type isEqualToString:@"com.dimi.diandi86.home"]) {
DimiViewController *dimiVC = [[DimiViewController alloc] init];
[self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
}];
} else if ([shortcutItem.type isEqualToString:@"com.dimi.diandi86.wonderful"]) {
DimiViewController *dimiVC = [[DimiViewController alloc] init];
[self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
}];
}
return NO;
}
return YES;
}
- (void)creatShortcutItem
{
//創建系統風格的icon
UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeShare];
// //創建自定義圖標的icon,圖標格式35x35像素單色
//UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"Wonderful.png"];
//創建快捷選項
UIApplicationShortcutItem * item = [[UIApplicationShortcutItem alloc] initWithType:@"com.dimi.diandi86.wonderful" localizedTitle:@"精彩" localizedSubtitle:nil icon:icon userInfo:nil];
//添加到快捷選項數組
[UIApplication sharedApplication].shortcutItems = @[item];
}
效果圖如下:
3D Touch 快捷標簽.png
2.3 點擊快捷選項標簽進入應用的響應
//如果app在后臺,通過快捷選項標簽進入app,則調用該方法,如果app不在后臺已殺死,則處理通過快捷選項標簽進入app的邏輯在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler
{
//判斷先前我們設置的快捷選項標簽唯一標識,根據不同標識執行不同操作
if (shortcutItem) {
//判斷先前我們設置的快捷選項標簽唯一標識,根據不同標識執行不同操作
if([shortcutItem.type isEqualToString:@"com.dimi.diandi86.main"]){
DimiViewController *dimiVC = [[DimiViewController alloc] init];
[self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
}];
} else if ([shortcutItem.type isEqualToString:@"com.dimi.diandi86.home"]) {
DimiViewController *dimiVC = [[DimiViewController alloc] init];
[self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
}];
} else if ([shortcutItem.type isEqualToString:@"com.dimi.diandi86.wonderful"]) {
DimiViewController *dimiVC = [[DimiViewController alloc] init];
[self.window.rootViewController presentViewController:dimiVC animated:YES completion:^{
}];
}
}
if (completionHandler) {
completionHandler(YES);
}
}
效果圖如下
點擊快捷標簽選項的響應.png
3 Peek and Pop 功能
應用啟動之后界面如下
3D Touch主界面.png
3.1 讓ViewController 遵守UIViewControllerPreviewingDelegate協議
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UIViewControllerPreviewingDelegate>
@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@end
3.2 給 cell 注冊 3D Touch 的 Peek and Pop 功能
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"cellIdentifier";
UITableViewCell * cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = [self.dataArray objectAtIndex: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;
}
3.3 實現 UIViewControllerPreviewingDelegate 方法
#pragma mark - UIViewControllerPreviewingDelegate method
//peek(預覽模式)
- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
//獲取按壓的cell所在行,[previewingContext sourceView]就是按壓的那個視圖
NSIndexPath *indexPath = [self.mainTableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];
//設定預覽的界面
DimiViewController *childVC = [[DimiViewController alloc] init];
childVC.preferredContentSize = CGSizeMake(0.0f,500.0f);
//調整不被虛化的范圍,按壓的那個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
{
DimiViewController *childVC = [[DimiViewController alloc] init];
[self.navigationController pushViewController:childVC animated:YES];
}
預覽界面效果圖如下
預覽界面效果圖.png
3.4 預覽界面向上滑動交互實現,在DimiViewController 中實現previewActionItems方法
- (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");
}];
NSArray *actions = @[action1,action2];
return actions;
}
效果圖如下
預覽交互效果圖.png
4 Force Properties 的運用
-(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 == 520) {
self.touchTipValueLabel.text = [NSString stringWithFormat:@"壓力值:%f",touch.force];
}
}
#pragma mark - getter and setter
- (UILabel*)touchLabel
{
if (_touchLabel == nil) {
_touchLabel = [[UILabel alloc] init];
_touchLabel.font = [UIFont systemFontOfSize:25];
_touchLabel.text = @"按下我";
_touchLabel.userInteractionEnabled = YES;
_touchLabel.textAlignment = NSTextAlignmentCenter;
_touchLabel.tag = 520;
}
return _touchLabel;
}
- (UILabel*)touchTipValueLabel
{
if (_touchTipValueLabel == nil) {
_touchTipValueLabel = [[UILabel alloc] init];
_touchTipValueLabel.font = [UIFont systemFontOfSize:25];
_touchTipValueLabel.text = @"壓力值:";
_touchTipValueLabel.textAlignment = NSTextAlignmentCenter;
}
return _touchTipValueLabel;
}
效果圖如下
壓力值的運用.png
3D Touch 學習就到此結束啦...