|
|
@@ -14,6 +14,8 @@
|
|
|
@property (strong, nonatomic) IBOutlet UILabel *indicator;
|
|
|
@property (strong, nonatomic) IBOutlet UICollectionView *previewContainer;
|
|
|
|
|
|
+@property (nonatomic,strong) UIScrollView *mask;
|
|
|
+
|
|
|
@end
|
|
|
|
|
|
@implementation NewPhotoPreviewController
|
|
|
@@ -35,34 +37,79 @@
|
|
|
NSString *offset = [NSString stringWithFormat:@"%lu / %lu",(unsigned long)self.currentIndex + 1,(unsigned long)self.photos.count];
|
|
|
self.indicator.text = offset;
|
|
|
|
|
|
-
|
|
|
+ // 添加Mask,使视图呈现时遮盖CollectionView滚动动画
|
|
|
+ [self.view insertSubview:self.mask belowSubview:self.indicator];
|
|
|
}
|
|
|
|
|
|
- (void)viewDidLayoutSubviews {
|
|
|
[super viewDidLayoutSubviews];
|
|
|
-//
|
|
|
-// UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.previewContainer.collectionViewLayout;
|
|
|
-// CGFloat w = CGRectGetWidth(self.view.frame);
|
|
|
-// CGFloat h = CGRectGetHeight(self.view.frame);
|
|
|
-// layout.itemSize = CGSizeMake(w,h);
|
|
|
-// layout.minimumLineSpacing = 0;
|
|
|
-// layout.minimumInteritemSpacing = 0;
|
|
|
+
|
|
|
+
|
|
|
+ if (self.currentIndex > 0) {
|
|
|
+ [self.previewContainer scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0] atScrollPosition:16 animated:YES];
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
- (void)viewDidAppear:(BOOL)animated {
|
|
|
[super viewDidAppear:animated];
|
|
|
|
|
|
- [self.previewContainer reloadData]; // 重新布局Item大小才正确
|
|
|
-
|
|
|
+// if (self.currentIndex > 0) {
|
|
|
+// [self.previewContainer scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:self.currentIndex inSection:0] atScrollPosition:32 animated:NO];
|
|
|
+// }
|
|
|
+ // 滚动动画完成后移除Mask
|
|
|
+ dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
|
|
+ sleep(0.3);
|
|
|
+ dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
+ [self.mask removeFromSuperview];
|
|
|
+ });
|
|
|
+
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
+- (BOOL)shouldAutorotate {
|
|
|
+ return NO;
|
|
|
+}
|
|
|
|
|
|
- (void)didReceiveMemoryWarning {
|
|
|
[super didReceiveMemoryWarning];
|
|
|
// Dispose of any resources that can be recreated.
|
|
|
}
|
|
|
|
|
|
+- (UIScrollView *)mask {
|
|
|
+ if (!_mask) {
|
|
|
+ UIScrollView *sc = [[UIScrollView alloc] initWithFrame:self.view.bounds];
|
|
|
+ sc.backgroundColor = [UIColor blackColor];
|
|
|
+ sc.delegate = self;
|
|
|
+ sc.maximumZoomScale = 5;
|
|
|
+ sc.showsVerticalScrollIndicator = NO;
|
|
|
+ sc.showsHorizontalScrollIndicator = NO;
|
|
|
+ NSDictionary *item = [self.photos objectAtIndex:self.currentIndex];
|
|
|
+ UIImage *img = [UIImage imageWithContentsOfFile:[item objectForKey:@"path"]];
|
|
|
+
|
|
|
+ CGFloat width = CGRectGetWidth(sc.bounds);
|
|
|
+ CGFloat height = CGRectGetHeight(sc.bounds);
|
|
|
+ CGFloat w = img.size.width;
|
|
|
+ CGFloat h = img.size.height;
|
|
|
+ // 根据图片大小和ScrollView大小等比缩放,使ScrollView容得下图片
|
|
|
+ float factor = MAX(w / width, h / height);
|
|
|
+ factor = 1 / factor * 0.8;
|
|
|
+
|
|
|
+ CGRect frame = CGRectMake((width - w * factor) * 0.5, (height - h * factor) * 0.5, w * factor, h * factor);
|
|
|
+ UIImageView *iv = [[UIImageView alloc] initWithImage:img];
|
|
|
+ iv.frame = frame;
|
|
|
+ iv.userInteractionEnabled = YES;
|
|
|
+ for (UIView *v in sc.subviews) {
|
|
|
+ [v removeFromSuperview];
|
|
|
+ }
|
|
|
+ [sc addSubview:iv];
|
|
|
+ sc.contentSize = CGSizeZero;
|
|
|
+ sc.contentOffset = CGPointZero;
|
|
|
+ _mask = sc;
|
|
|
+ }
|
|
|
+ return _mask;
|
|
|
+}
|
|
|
+
|
|
|
#pragma mark - Setter
|
|
|
|
|
|
- (void)setCurrentIndex:(NSUInteger)currentIndex {
|
|
|
@@ -94,7 +141,6 @@
|
|
|
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
PhotoPreviewCell *preCell = (PhotoPreviewCell *)cell;
|
|
|
|
|
|
- self.currentIndex = indexPath.row;
|
|
|
|
|
|
UIScrollView *sc = preCell.scrollView;
|
|
|
sc.delegate = self;
|
|
|
@@ -151,6 +197,18 @@
|
|
|
|
|
|
#pragma mark - ScrollView Delegate
|
|
|
|
|
|
+- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
|
|
|
+ if (scrollView == self.previewContainer) {
|
|
|
+ CGFloat x = scrollView.contentOffset.x / scrollView.frame.size.width;
|
|
|
+ int idx = (int)x;
|
|
|
+ if (idx == x) {
|
|
|
+ if (self.currentIndex != idx) {
|
|
|
+ self.currentIndex = idx;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
|
|
|
if (scrollView != self.previewContainer) {
|
|
|
return scrollView.subviews.firstObject;
|