Advertisement
axelso

PhotoPreviewCollectionViewCell.m (K)

Nov 9th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  PhotoPreviewCollectionViewCell.m
  3. //  Moselo
  4. //
  5. //  Created by Dominic Vedericho on 04/10/17.
  6. //  Copyright © 2017 Moselo. All rights reserved.
  7. //
  8.  
  9. #import "PhotoPreviewCollectionViewCell.h"
  10. #import "RNImageView.h"
  11.  
  12. @interface PhotoPreviewCollectionViewCell () <UIScrollViewDelegate, RNImageViewDelegate>
  13.  
  14. @property (strong, nonatomic) UIScrollView *scrollView;
  15. @property (strong, nonatomic) RNImageView *portfolioImageView;
  16. @property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
  17. @property (nonatomic) BOOL isLoading;
  18. @property (nonatomic) CGFloat imageURLWidth;
  19. @property (nonatomic) CGFloat imageURLHeight;
  20.  
  21. //AS TEMP UIView outline
  22. @property (strong, nonatomic) UIView *topAreaView;
  23. @property (strong, nonatomic) UIView *croppedAreaView;
  24. @property (strong, nonatomic) UIView *bottomAreaView;
  25. //END AS TEMP
  26.  
  27. - (void)doubleTap:(UIGestureRecognizer *)gestureRecognizer;
  28. - (void)centerScrollViewContents;
  29. - (void)refreshImageWithImage:(UIImage *)image;
  30. - (void)refreshImageFromImageURL;
  31.  
  32. @end
  33.  
  34. @implementation PhotoPreviewCollectionViewCell
  35. #pragma mark - Life Cycle
  36. - (id) initWithFrame:(CGRect)frame {
  37.     self = [super initWithFrame:frame];
  38.     if(self) {
  39.        
  40.         self.contentView.backgroundColor = [UIColor whiteColor];
  41.         [self.activityIndicator startAnimating];
  42.        
  43.         _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
  44.         self.scrollView.delegate = self;
  45.         self.scrollView.showsVerticalScrollIndicator = NO;
  46.         self.scrollView.showsHorizontalScrollIndicator = NO;
  47. //        self.scrollView.maximumZoomScale = 1.0f;
  48.         [self.contentView addSubview:self.scrollView];
  49.        
  50.         _portfolioImageView = [[RNImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
  51.         self.portfolioImageView.clipsToBounds = YES;
  52.         self.portfolioImageView.delegate = self;
  53.         self.portfolioImageView.contentMode = UIViewContentModeScaleAspectFill;
  54.         [self.scrollView addSubview:self.portfolioImageView];
  55.        
  56.         //AS TEMP CROPPED VIEW
  57.         _croppedAreaView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
  58.         self.croppedAreaView.clipsToBounds = YES;
  59.         self.croppedAreaView.backgroundColor = [UIColor greenColor];
  60.         self.croppedAreaView.alpha = 0.5f;
  61.         [self.portfolioImageView addSubview:self.croppedAreaView];
  62.         //END AS TEMP CROPPED VIEW
  63.        
  64.         UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
  65.         tapTwice.numberOfTapsRequired = 2;
  66.         [self.contentView addGestureRecognizer:tapTwice];
  67.        
  68.         _isLoading = YES;
  69.     }
  70.     return self;
  71. }
  72.  
  73. - (void)prepareForReuse {
  74.     [super prepareForReuse];
  75.     self.portfolioImageView.image = nil;
  76. }
  77.  
  78. #pragma mark - Delegate
  79. #pragma mark RNImageView
  80. - (void)RNImageViewDidFinishLoadImage:(RNImageView *)imageView {
  81.     _isLoading = NO;
  82.     [self refreshImageFromImageURL];
  83.     [self.activityIndicator stopAnimating];
  84. }
  85.  
  86. #pragma mark UIScrollView
  87. - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  88.     return self.portfolioImageView;
  89. }
  90.  
  91. - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  92.     [self centerScrollViewContents];
  93. }
  94.  
  95. - (void)setImageWithURLData:(AvatarURLModel *)urlData {
  96.     NSString *urlString = urlData.large;
  97.     if(urlData.largeImageWidth == nil){
  98.         self.imageURLWidth = 100.0f;
  99.     }
  100.     else {
  101.        self.imageURLWidth = [urlData.largeImageWidth floatValue];
  102.     }
  103.    
  104.     if(urlData.largeImageHeight == nil){
  105.         self.imageURLHeight = 100.0f;
  106.     }
  107.     else {
  108.         self.imageURLHeight = [urlData.largeImageHeight floatValue];
  109.     }
  110.     [self.portfolioImageView setImageWithURLString:urlString];
  111. }
  112.  
  113. - (void)setImageWithURL:(NSString *)imageURL {
  114.     NSString *urlString = imageURL;
  115.     [self.portfolioImageView setImageWithURLString:urlString];
  116. }
  117.  
  118. - (void)setImageWithImageData:(UIImage *)image {
  119.     _isLoading = NO;
  120.     [self.portfolioImageView setImage:image];
  121.     [self refreshImageWithImage:image];
  122. }
  123.  
  124. - (void)refreshImageWithImage:(UIImage *)image {
  125.     self.portfolioImageView.frame = CGRectZero;
  126.    
  127.     //AS TEMP
  128.     self.croppedAreaView.frame = CGRectZero;
  129.     //END AS TEMP
  130.    
  131.     self.scrollView.contentSize = self.portfolioImageView.frame.size;
  132.     self.scrollView.minimumZoomScale = 1.0f;
  133.     self.scrollView.maximumZoomScale = 1.0f;
  134.     self.scrollView.zoomScale = 1.0f;
  135.    
  136.     CGFloat photoZoomWidth = image.size.width;
  137.     CGFloat photoZoomHeight = image.size.height;
  138.     //AS TEMP
  139.     //self.croppedAreaView.frame = CGRectMake(0.0f, ((image.size.height) - 205.0f) / 2.0f, image.size.width, 205.0f);
  140.     //END AS TEMP
  141.     //=========
  142.     //AS TEMP
  143.     self.croppedAreaView.frame = CGRectMake(0.0f, ((photoZoomHeight) - ((image.size.width * 9.0f) / 16.0f)) / 2.0f, image.size.width, (image.size.width * 9.0f) / 16.0f);
  144.     //END AS TEMP
  145.    
  146.     //AS TEMP topAreaView
  147. //    _topAreaView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, CGRectGetMinY(self.portfolioImageView.frame) - (CGRectGetHeight(self.portfolioImageView.frame) - CGRectGetHeight(self.croppedAreaView.frame)) / 2.0f , photoZoomWidth, (CGRectGetHeight(self.portfolioImageView.frame) - CGRectGetHeight(self.croppedAreaView.frame)) / 2.0f )];
  148. //    self.topAreaView.backgroundColor = [UIColor redColor];
  149. //    self.topAreaView.alpha = 0.5f;
  150. //    [self.portfolioImageView addSubview:self.topAreaView];
  151.     //END AS TEMP topAreaView
  152.    
  153.     //AS TEMP bottomAreaView
  154. //    _bottomAreaView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, CGRectGetMaxY(self.portfolioImageView.frame) + 205.0f, photoZoomWidth, (CGRectGetHeight(self.portfolioImageView.frame) - CGRectGetHeight(self.croppedAreaView.frame)) / 2.0f )];
  155. //    self.topAreaView.backgroundColor = [UIColor redColor];
  156. //    self.topAreaView.alpha = 0.5f;
  157. //    [self.portfolioImageView addSubview:self.topAreaView];
  158.     //END AS TEMP bottomAreaView
  159.    
  160.     BOOL isPortrait = NO;
  161.    
  162.     if(photoZoomHeight > photoZoomWidth) {
  163.         isPortrait = YES;
  164.     }
  165.    
  166.     CGRect scrollViewFrame = self.scrollView.frame;
  167.    
  168.     if(isPortrait) {
  169.         if(photoZoomHeight < CGRectGetHeight(scrollViewFrame)) {
  170.             photoZoomWidth = (CGRectGetHeight(scrollViewFrame)/photoZoomHeight) * photoZoomWidth;
  171.             photoZoomHeight = CGRectGetHeight(scrollViewFrame);
  172.         }
  173.     }
  174.     else {
  175.         if(photoZoomWidth < CGRectGetWidth(scrollViewFrame)) {
  176.             photoZoomHeight = (CGRectGetWidth(scrollViewFrame)/photoZoomWidth) * photoZoomHeight;
  177.             photoZoomWidth = CGRectGetWidth(scrollViewFrame);
  178.         }
  179.     }
  180.    
  181.     self.portfolioImageView.frame = CGRectMake(0.0f, 0.0f, photoZoomWidth, photoZoomHeight);
  182. //    //AS TEMP
  183. //    self.croppedAreaView.frame = CGRectMake(0.0f, ((photoZoomHeight) - (photoZoomHeight - 410.0f)) / 2.0f, image.size.width, photoZoomHeight - 410.0f);
  184. //    //END AS TEMP
  185.     self.scrollView.contentSize = self.portfolioImageView.frame.size;
  186.    
  187.     CGFloat scaleWidth = CGRectGetWidth(scrollViewFrame) / self.scrollView.contentSize.width;
  188.     CGFloat scaleHeight = CGRectGetHeight(scrollViewFrame) / self.scrollView.contentSize.height;
  189.    
  190.     CGFloat minimumZoomScale = scaleWidth;
  191.    
  192.     if((self.scrollView.contentSize.width * scaleHeight) < CGRectGetWidth([UIScreen mainScreen].bounds)) {
  193.         minimumZoomScale = scaleHeight;
  194.     }
  195.    
  196.     if(!self.isLoading) {
  197.         self.scrollView.minimumZoomScale = minimumZoomScale;
  198.         self.scrollView.maximumZoomScale = 1.0f;
  199.         self.scrollView.zoomScale = minimumZoomScale;
  200.     }
  201.    
  202.     [self centerScrollViewContents];
  203. }
  204.  
  205. - (void)refreshImageFromImageURL {
  206.     self.portfolioImageView.frame = CGRectZero;
  207.    
  208.     //AS TEMP
  209.     self.croppedAreaView.frame = CGRectZero;
  210.     //END AS TEMP
  211.    
  212.     self.scrollView.contentSize = self.portfolioImageView.frame.size;
  213.     self.scrollView.minimumZoomScale = 1.0f;
  214.     self.scrollView.maximumZoomScale = 1.0f;
  215.     self.scrollView.zoomScale = 1.0f;
  216.    
  217.     CGFloat photoZoomWidth = self.imageURLWidth;
  218.     CGFloat photoZoomHeight = self.imageURLHeight;
  219. //    //AS TEMP
  220. //    self.croppedAreaView.frame = CGRectMake(0.0f, ((photoZoomHeight) - (photoZoomHeight - 410.0f)) / 2.0f, self.imageURLWidth, photoZoomHeight - 410.0f);
  221. //    //END AS TEMP
  222.    
  223.     //AS TEMP
  224.     self.croppedAreaView.frame = CGRectMake(0.0f, ((self.imageURLHeight) - ((self.imageURLWidth * 9.0f) / 16.0f)) / 2.0f, self.imageURLWidth, (self.imageURLWidth * 9.0f) / 16.0f);
  225.     //END AS TEMP
  226.    
  227.     BOOL isPortrait = NO;
  228.    
  229.     if(photoZoomHeight > photoZoomWidth) {
  230.         isPortrait = YES;
  231.     }
  232.    
  233.     CGRect scrollViewFrame = self.scrollView.frame;
  234.    
  235.     if(isPortrait) {
  236.         if(photoZoomHeight < CGRectGetHeight(scrollViewFrame)) {
  237.             photoZoomWidth = (CGRectGetHeight(scrollViewFrame)/photoZoomHeight) * photoZoomWidth;
  238.             photoZoomHeight = CGRectGetHeight(scrollViewFrame);
  239.         }
  240.     }
  241.     else {
  242.         if(photoZoomWidth < CGRectGetWidth(scrollViewFrame)) {
  243.             photoZoomHeight = (CGRectGetWidth(scrollViewFrame)/photoZoomWidth) * photoZoomHeight;
  244.             photoZoomWidth = CGRectGetWidth(scrollViewFrame);
  245.         }
  246.     }
  247.    
  248.     self.portfolioImageView.frame = CGRectMake(0.0f, 0.0f, photoZoomWidth, photoZoomHeight);
  249.     //AS TEMP
  250.     self.croppedAreaView.frame = CGRectMake(0.0f, ((photoZoomHeight) - (photoZoomHeight - 410.0f)) / 2.0f, self.imageURLWidth, photoZoomHeight - 410.0f);
  251.     //END AS TEMP
  252.     self.scrollView.contentSize = self.portfolioImageView.frame.size;
  253.    
  254.     CGFloat scaleWidth = CGRectGetWidth(scrollViewFrame) / self.scrollView.contentSize.width;
  255.     CGFloat scaleHeight = CGRectGetHeight(scrollViewFrame) / self.scrollView.contentSize.height;
  256.    
  257.     CGFloat minimumZoomScale = scaleWidth;
  258.    
  259.     if((self.scrollView.contentSize.width * scaleHeight) < CGRectGetWidth([UIScreen mainScreen].bounds)) {
  260.         minimumZoomScale = scaleHeight;
  261.     }
  262.    
  263.     if(!self.isLoading) {
  264.         self.scrollView.minimumZoomScale = minimumZoomScale;
  265.         self.scrollView.maximumZoomScale = 1.0f;
  266.         self.scrollView.zoomScale = minimumZoomScale;
  267.     }
  268.    
  269.     [self centerScrollViewContents];
  270. }
  271.  
  272. - (void)doubleTap:(UIGestureRecognizer *)gestureRecognizer {
  273.    
  274.     CGFloat maximumDoubleTapZoom = (self.scrollView.maximumZoomScale - self.scrollView.minimumZoomScale) / 2.0f;
  275.    
  276.     if(self.scrollView.zoomScale >= maximumDoubleTapZoom) {
  277.         [self.scrollView setZoomScale:self.scrollView.minimumZoomScale animated:YES];
  278.        
  279.         return;
  280.     }
  281.    
  282.     CGPoint pointInView = [gestureRecognizer locationInView:self.portfolioImageView];
  283.    
  284.     CGFloat newZoomScale = maximumDoubleTapZoom;
  285.    
  286.     CGSize scrollViewSize = self.scrollView.bounds.size;
  287.    
  288.     CGFloat width = scrollViewSize.width / newZoomScale;
  289.     CGFloat height = scrollViewSize.height / newZoomScale;
  290.     CGFloat x = pointInView.x - (width / 2.0f);
  291.     CGFloat y = pointInView.y - (height / 2.0f);
  292.    
  293.     CGRect zoomRect = CGRectMake(x, y, width, height);
  294.    
  295.     [self.scrollView zoomToRect:zoomRect animated:YES];
  296. }
  297.  
  298. - (void)centerScrollViewContents {
  299.     CGSize boundsSize = self.scrollView.bounds.size;
  300.     CGRect contentsFrame = self.portfolioImageView.frame;
  301.    
  302.     if (contentsFrame.size.width < boundsSize.width) {
  303.         contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
  304.     } else {
  305.         contentsFrame.origin.x = 0.0f;
  306.     }
  307.    
  308.     if (contentsFrame.size.height < boundsSize.height) {
  309.         contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
  310.     } else {
  311.         contentsFrame.origin.y = 0.0f;
  312.     }
  313.    
  314.     self.portfolioImageView.frame = contentsFrame;
  315. }
  316.  
  317. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement