Browse Source

1.修改NewPreview根据索引呈现视图,禁止屏幕旋转

Pen Li 9 năm trước cách đây
mục cha
commit
14347ba399

BIN
RA Image/RA Image.xcodeproj/project.xcworkspace/xcuserdata/macmini1.xcuserdatad/UserInterfaceState.xcuserstate


+ 69 - 11
RA Image/RA Image/NewPhotoPreviewController.m

@@ -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;

+ 0 - 3
RA Image/RA Image/PhotoList.storyboard

@@ -201,7 +201,6 @@
                         <subviews>
                             <collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="eUk-3I-LIa">
                                 <rect key="frame" x="0.0" y="0.0" width="414" height="736"/>
-                                <color key="backgroundColor" red="1" green="0.053315131760000002" blue="0.038905751279999998" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                 <collectionViewFlowLayout key="collectionViewLayout" scrollDirection="horizontal" minimumLineSpacing="10" minimumInteritemSpacing="10" id="AhJ-66-sid">
                                     <size key="itemSize" width="414" height="735"/>
                                     <size key="headerReferenceSize" width="0.0" height="0.0"/>
@@ -218,11 +217,9 @@
                                             <subviews>
                                                 <scrollView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="5Dl-d3-MLq">
                                                     <rect key="frame" x="0.0" y="0.0" width="414" height="735"/>
-                                                    <color key="backgroundColor" red="0.064155410240000002" green="1" blue="0.59383154270000005" alpha="1" colorSpace="calibratedRGB"/>
                                                 </scrollView>
                                             </subviews>
                                         </view>
-                                        <color key="backgroundColor" red="0.2190771869" green="0.711363619" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                         <constraints>
                                             <constraint firstItem="5Dl-d3-MLq" firstAttribute="width" secondItem="eAR-El-bRn" secondAttribute="width" id="OwB-eR-E1R"/>
                                             <constraint firstItem="5Dl-d3-MLq" firstAttribute="centerY" secondItem="eAR-El-bRn" secondAttribute="centerY" id="hQv-Uz-CBO"/>

+ 4 - 0
RA Image/RA Image/RootNavigationController.m

@@ -8,6 +8,7 @@
 
 #import "RootNavigationController.h"
 #import "PhotoPreviewController.h"
+#import "NewPhotoPreviewController.h"
 
 @interface RootNavigationController ()
 
@@ -29,6 +30,9 @@
     if ([self.topViewController isKindOfClass:[PhotoPreviewController class]]) { // 图片预览保持竖屏
         return NO;
     }
+    if ([self.topViewController isKindOfClass:[NewPhotoPreviewController class]]) { // 图片预览保持竖屏
+        return NO;
+    }
     return YES;
 }