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

非 3D Touch 設備上模擬 Peek 預覽效果

編輯:iOS 開發基礎

Peek 和 Pop 是很實用的交互,讓用戶不必進入二級頁面就能快速預覽內容,遺憾的是它依賴 3D Touch,只能在部分設備上使用。

STPopupPreview 的思路很巧妙:用长按手勢(Long Press Gesture)在非 3D Touch 設備上復刻這套體驗——長按後抬手即可彈出預覽,並支持上滑喚出 Preview Actions。實際效果可以參考 demo:https://github.com/kevin0571/STPopupPreview 或者 Instagram 的 iOS App。


STPopupPreview 基於 STPopup 開發,同時支持 CocoaPods 與 Carthage 依賴管理。

GitHub 地址(含示例):https://github.com/kevin0571/STPopupPreview

效果圖:

基本用法

第一步,給目標視圖綁定 preview recognizer:

CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([CollectionViewCell class])
                                                            forIndexPath:indexPath];
if (!cell.popupPreviewRecognizer) {
    cell.popupPreviewRecognizer = [[STPopupPreviewRecognizer alloc] initWithDelegate:self];
}

第二步,實現對應的 Delegate 方法。

返回用於預覽的 view controller:

- (UIViewController *)previewViewControllerForPopupPreviewRecognizer:(STPopupPreviewRecognizer *)popupPreviewRecognizer {
    if (![popupPreviewRecognizer.view isKindOfClass:[CollectionViewCell class]]) {
        return nil;
    }

    CollectionViewCell *cell = popupPreviewRecognizer.view;
    PreviewViewController *previewViewController = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([PreviewViewController class])];
    previewViewController.data = cell.data;
    previewViewController.placeholderImage = cell.imageView.image;

    return previewViewController;
}

返回負責 present 預覽頁的 view controller,一般就是 self:

- (UIViewController *)presentingViewControllerForPopupPreviewRecognizer:(STPopupPreviewRecognizer *)popupPreviewRecognizer {
    return self;
}

返回 preview actions,可以為空:

- (NSArray *)previewActionsForPopupPreviewRecognizer:(STPopupPreviewRecognizer *)popupPreviewRecognizer {
    return @[
        [STPopupPreviewAction actionWithTitle:@"Like"
                                        style:STPopupPreviewActionStyleDefault
                                      handler:^(STPopupPreviewAction *action, UIViewController *previewViewController) {
            // Action handler
        }]
    ];
}

整套接入下來,代碼量很小,但交互手感與系統的 Peek 幾乎一致,對不支持 3D Touch 的設備來說是個不錯的補償方案。

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