Last active
January 13, 2017 15:05
-
-
Save frosty/e3353018850cd21a6e8756ccdc5f7aab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/Pod/Classes/WPFullScreenAssetPreviewViewController.m b/Pod/Classes/WPFullScreenAssetPreviewViewController.m | |
index 3765e0c..8ad225d 100644 | |
--- a/Pod/Classes/WPFullScreenAssetPreviewViewController.m | |
+++ b/Pod/Classes/WPFullScreenAssetPreviewViewController.m | |
@@ -143,4 +143,9 @@ - (void)handleTapOnAsset:(UIGestureRecognizer *)gestureRecognizer | |
} | |
} | |
+- (CGSize)preferredContentSize | |
+{ | |
+ return [self.asset pixelSize]; | |
+} | |
+ | |
@end | |
diff --git a/Pod/Classes/WPMediaCollectionDataSource.h b/Pod/Classes/WPMediaCollectionDataSource.h | |
index 4d14793..1667480 100644 | |
--- a/Pod/Classes/WPMediaCollectionDataSource.h | |
+++ b/Pod/Classes/WPMediaCollectionDataSource.h | |
@@ -137,6 +137,13 @@ typedef int32_t WPMediaRequestID; | |
*/ | |
- (NSDate *)date; | |
+/** | |
+ * The size, in pixels, of the asset’s image or video data. | |
+ * | |
+ * @return The size, in pixels, of the asset’s image or video data. | |
+ */ | |
+- (CGSize)pixelSize; | |
+ | |
@end | |
/** | |
diff --git a/Pod/Classes/WPMediaCollectionViewController.m b/Pod/Classes/WPMediaCollectionViewController.m | |
index 0414926..eb52b45 100644 | |
--- a/Pod/Classes/WPMediaCollectionViewController.m | |
+++ b/Pod/Classes/WPMediaCollectionViewController.m | |
@@ -14,7 +14,8 @@ @interface WPMediaCollectionViewController () | |
UINavigationControllerDelegate, | |
WPMediaGroupPickerViewControllerDelegate, | |
UIPopoverPresentationControllerDelegate, | |
- UICollectionViewDelegateFlowLayout | |
+ UICollectionViewDelegateFlowLayout, | |
+ UIViewControllerPreviewingDelegate | |
> | |
@property (nonatomic, strong) UICollectionViewFlowLayout *layout; | |
@@ -107,7 +108,13 @@ - (void)viewDidLoad | |
}); | |
}]; | |
- [self.view addGestureRecognizer:self.longPressGestureRecognizer]; | |
+ | |
+ if ([self.traitCollection containsTraitsInCollection:[UITraitCollection traitCollectionWithForceTouchCapability:UIForceTouchCapabilityAvailable]]) { | |
+ [self registerForPreviewingWithDelegate:self sourceView:self.view]; | |
+ } else { | |
+ [self.view addGestureRecognizer:self.longPressGestureRecognizer]; | |
+ } | |
+ | |
[self refreshData]; | |
} | |
@@ -737,19 +744,50 @@ - (void)mediaGroupPickerViewControllerDidCancel:(WPMediaGroupPickerViewControlle | |
- (void)handleLongPressOnAsset:(UIGestureRecognizer *)gestureRecognizer { | |
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { | |
- WPFullScreenAssetPreviewViewController *fullScreenImageVC = [[WPFullScreenAssetPreviewViewController alloc] init]; | |
- CGPoint pointTouched = [gestureRecognizer locationInView:self.collectionView]; | |
- NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:pointTouched]; | |
- if (!indexPath) { | |
- return; | |
- } | |
- id<WPMediaAsset> asset = [self assetForPosition:indexPath]; | |
- if (!asset) { | |
- return; | |
+ CGPoint location = [gestureRecognizer locationInView:self.collectionView]; | |
+ UIViewController *viewController = [self fullscreenAssetPreviewControllerForTouchLocation:location]; | |
+ | |
+ if (viewController) { | |
+ [self.navigationController pushViewController:viewController animated:YES]; | |
} | |
- fullScreenImageVC.asset = asset; | |
- [self.navigationController pushViewController:fullScreenImageVC animated:YES]; | |
} | |
} | |
+- (nullable WPFullScreenAssetPreviewViewController *)fullscreenAssetPreviewControllerForTouchLocation:(CGPoint)location | |
+{ | |
+ NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:location]; | |
+ if (!indexPath) { | |
+ return nil; | |
+ } | |
+ | |
+ id<WPMediaAsset> asset = [self assetForPosition:indexPath]; | |
+ if (!asset) { | |
+ return nil; | |
+ } | |
+ | |
+ WPFullScreenAssetPreviewViewController *fullScreenImageVC = [[WPFullScreenAssetPreviewViewController alloc] init]; | |
+ fullScreenImageVC.asset = asset; | |
+ return fullScreenImageVC; | |
+} | |
+ | |
+#pragma mark - UIViewControllerPreviewingDelegate | |
+ | |
+- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location | |
+{ | |
+ CGPoint convertedLocation = [self.collectionView convertPoint:location fromView:self.view]; | |
+ NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:convertedLocation]; | |
+ if (indexPath) { | |
+ UICollectionViewLayoutAttributes *attributes = [self.collectionView layoutAttributesForItemAtIndexPath:indexPath]; | |
+ CGRect rect = [self.view convertRect:attributes.frame fromView:self.collectionView]; | |
+ [previewingContext setSourceRect:rect]; | |
+ } | |
+ | |
+ return [self fullscreenAssetPreviewControllerForTouchLocation:convertedLocation]; | |
+} | |
+ | |
+- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit | |
+{ | |
+ [self.navigationController pushViewController:viewControllerToCommit animated:YES]; | |
+} | |
+ | |
@end | |
diff --git a/Pod/Classes/WPPHAssetDataSource.m b/Pod/Classes/WPPHAssetDataSource.m | |
index 6625f21..5b079d6 100644 | |
--- a/Pod/Classes/WPPHAssetDataSource.m | |
+++ b/Pod/Classes/WPPHAssetDataSource.m | |
@@ -462,6 +462,11 @@ - (NSDate *)date | |
return [self creationDate]; | |
} | |
+- (CGSize)pixelSize | |
+{ | |
+ return CGSizeMake((CGFloat)self.pixelWidth, (CGFloat)self.pixelHeight); | |
+} | |
+ | |
@end | |
#pragma mark - WPPHAssetCollection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment