您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發基礎

【譯】Instagram 的 3D Touch 實戰經驗:Quick Actions 與 Peek 該怎麼做

編輯:iOS 開發基礎

本文由 CocoaChina 譯者 Leon 翻譯自 Instagram 工程師 Ryan Nystrom 的部落格文章《Lessons learned with 3D Touch》。


Apple 在 iPhone 6s 這一代加入了 3D Touch 硬體:螢幕下的壓力感測器能分辨「用力按壓」的動作,並回報相對精確的力度變化。伴隨硬體,Apple 同步開放三組 API——從桌面圖標直接進入功能的 Quick Actions、無須跳頁即可窺看內容的 Peek and Pop,以及透過 UITouch 取得按壓力道與 maximumPossibleForce 的能力。

Instagram 有幸在功能發表初期就完成整合。加上 shortcuts 與 peeking 之後,照片與影片的瀏覽體驗自然得令人驚喜。以下是我們在落地過程中的整理。

Quick Actions:把常用入口搬到桌面

Quick Actions 的價值在於省略中間步驟。我們把使用頻率最高的四項功能放了上去:Search、Direct、Activity 與 Creation。

實作相當輕量:建立 UIApplicationShortcutItem 陣列並掛到 app delegate 即可。每個 item 需要一個代表類型的字串,以及可選的 UIApplicationShortcutIcon。

+ (instancetype)ig_searchItem {
    NSString *title = NSLocalizedString(@"Search", nil);
    UIApplicationShortcutIcon *icon =
        [UIApplicationShortcutIcon iconWithTemplateImageName:@"search"];
    return [[UIApplicationShortcutItem alloc]
        initWithType:@"search"
        localizedTitle:title
        localizedSubtitle:nil
        icon:icon
        userInfo:nil];
}

我們會依登入狀態動態決定要顯示哪些項目:

application.shortcutItems = @[
    [UIApplicationShortcutItem ig_postItem],
    [UIApplicationShortcutItem ig_activityItem],
    [UIApplicationShortcutItem ig_searchItem],
    [UIApplicationShortcutItem ig_directItem]
];

由於既有架構早已處理 Universal Link 與推送通知的路由,我們只需在 -application:performActionForShortcutItem:completionHandler: 中呼叫對應流程,不必額外鋪一條路。

Peek and Pop:先看一眼,再決定要不要進去

就像拿放大鏡靠近物體,Peek 讓使用者取得更多資訊,卻不必真的載入完整內容。我們先把這個能力加在縮圖與影片上。

相關 API 是 UIViewControllerPreviewingDelegate,以 view 為單位註冊。我們只把它掛在真正能接收觸碰事件的 controller 上;當 3D Touch 觸發時,由 delegate 判斷被按壓的是哪個元素。

若判定確實該 Peek,delegate 還得做兩件事:設定預覽動作的 source rect,以及回傳要展示的 view controller。

系統會把包含 sourceView 與按壓 point 的 context 交給你,你再用這個 point 反查對應的 view 與資料。Instagram 的頁面大多建立在 UICollectionView 與 UITableView 之上,而兩者本身就有成熟的「座標 → 資料」映射 API:

- (UIViewController *)previewingContext:(id)previewingContext
              viewControllerForLocation:(CGPoint)location {
    UICollectionView *collectionView = (UICollectionView *)[previewingContext sourceView];
    NSIndexPath *indexPath = [collectionView indexPathForItemAtPoint:location];
    if (indexPath) {
        IGPostCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
        [previewingContext setSourceRect:cell.frame];

        IGPost *post = self.feedController.posts[indexPath.row];
        return [[IGThumbnailPreviewController alloc] initWithPost:post];
    }
    return nil;   // 不觸發 Peek
}

把預覽 API 接到既有的 view 與資料結構上之後,查看照片與影片變得毫不費力。

我們也為頁首與留言中的用戶名稱加上查看個人資料的能力,思路與照片預覽幾乎一致:

  • 用按壓 point 與查得的 indexPath 找到對應的 cell。

  • 把 point 轉換到 cell 內 text view 的座標系。

  • 取出該位置 attributed string 的屬性。

  • 若屬性指向使用者名稱,回傳 IGUserPreviewController。

混合內容:用 composition 而非複製貼上

Activity feed 同時混雜了用戶標記與縮圖。我們原本已各自寫過兩套 previewing delegate,此時最直覺的做法是把其中一套複製過來改,但那會製造兩份需要同步維護的程式碼。我們選擇用組合的方式包一層:

@interface IGUserThumbnailPreviewingHandler : NSObject
@property (nonatomic, strong) IGUserPreviewingHandler *userDelegate;
@property (nonatomic, strong) IGFeedThumbnailPreviewingHandler *thumbnailDelegate;
@end

- (UIViewController *)previewingContext:(id)previewingContext
              viewControllerForLocation:(CGPoint)location {
    id controller = [self.thumbnailDelegate previewingContext:previewingContext
                                        viewControllerForLocation:location];
    if (!controller) {
        controller = [self.userDelegate previewingContext:previewingContext
                                   viewControllerForLocation:location];
    }
    return controller;
}

關於預覽用的 view controller

它本質上就是 UIViewController 的子類,差別在幾個細節。

一開始它完全無法互動——你點不到上面的按鈕,也掛不上自訂手勢。要讓它可操作,必須回傳一組遵循 UIPreviewActionItem 協議的物件,我們只用 UIPreviewAction,寫法與 UIAlertController 的 action 幾乎相同。

數據策略上,我們維持全 app 一致的慣例:只在需要時載入,載入後快取備用,既不讓使用者乾等,也不浪費頻寬。有些時候預覽個人資料時拿到的資料還不足以顯示最新照片或追蹤數,這時仍可放心依賴 viewDidLoad、viewWillAppear: 等生命週期去補抓資料並刷新 UI。

最後一個實用發現:即使 controller 已經顯示,修改 preferredContentSize 仍然能改變預覽視窗大小;但這個屬性對動畫過程無效。

結語

對 Instagram 來說,3D Touch 就是 2015 年的「右鍵」。它引入了另一層意圖:使用者未必準備好跳頁,但一定對眼前內容感興趣——Peek 正好滿足了這份好奇,還能讓人輕鬆退回原本的瀏覽位置。Quick Actions 則把「發一篇新帖」「看看最近發生了什麼」壓縮到一次按壓,讓 app 在極簡的路徑下更好用。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved