STPopupPreview uses long press gesture to enable quick preview of a page on non 3D Touch devices. Preview actions are also supported. This idea is inspired by Instagram.
It is built on top of of STPopup(a library provides STPopupController, which works just like UINavigationController in popup style). Both STPopup and STPopupPreview support iOS 7+.
CocoaPods
platform: ios, '7.0'
pod 'STPopupPreview'
Carthage
github "kevin0571/STPopupPreview"
*Don't forget to drag both STPopupPreview.framework and STPopup.framework into linked frameworks.
#import <STPopupPreview/STPopupPreview.h>
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([CollectionViewCell class]) forIndexPath:indexPath];
if (!cell.popupPreviewRecognizer) {
cell.popupPreviewRecognizer = [[STPopupPreviewRecognizer alloc] initWithDelegate:self];
}
Return the preview view controller. The preview view controller should have "contentSizeInPopup" set before its "viewDidLoad" called. More about this please read the document of STPopup.
- (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;
}
Return a view controller to present the preview view controller. Most of the time it will be the current view controller.
- (UIViewController *)presentingViewControllerForPopupPreviewRecognizer:(STPopupPreviewRecognizer *)popupPreviewRecognizer
{
return self;
}
Return the preview actions you want to show when slides up. It can be nil if you don't have any preview actions.
- (NSArray<STPopupPreviewAction *> *)previewActionsForPopupPreviewRecognizer:(STPopupPreviewRecognizer *)popupPreviewRecognizer
{
return @[ [STPopupPreviewAction actionWithTitle:@"Like" style:STPopupPreviewActionStyleDefault handler:^(STPopupPreviewAction *action, UIViewController *previewViewController) {
// Action handler
}] ];
}
BOOL isForceTouchAvailable = [self respondsToSelector:@selector(traitCollection)] &&
[self.traitCollection respondsToSelector:@selector(forceTouchCapability)] &&
self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable;
if (!isForceTouchAvailable) {
if (!cell.popupPreviewRecognizer) {
cell.popupPreviewRecognizer = [[STPopupPreviewRecognizer alloc] initWithDelegate:self];
}
}