Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // PhotoPreviewCollectionViewCell.m
- // Moselo
- //
- // Created by Dominic Vedericho on 04/10/17.
- // Copyright © 2017 Moselo. All rights reserved.
- //
- #import "PhotoPreviewCollectionViewCell.h"
- #import "RNImageView.h"
- @interface PhotoPreviewCollectionViewCell () <UIScrollViewDelegate, RNImageViewDelegate>
- @property (strong, nonatomic) UIScrollView *scrollView;
- @property (strong, nonatomic) RNImageView *portfolioImageView;
- @property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
- @property (nonatomic) BOOL isLoading;
- @property (nonatomic) CGFloat imageURLWidth;
- @property (nonatomic) CGFloat imageURLHeight;
- //AS TEMP UIView outline
- @property (strong, nonatomic) UIView *topAreaView;
- @property (strong, nonatomic) UIView *croppedAreaView;
- @property (strong, nonatomic) UIView *bottomAreaView;
- //END AS TEMP
- - (void)doubleTap:(UIGestureRecognizer *)gestureRecognizer;
- - (void)centerScrollViewContents;
- - (void)refreshImageWithImage:(UIImage *)image;
- - (void)refreshImageFromImageURL;
- @end
- @implementation PhotoPreviewCollectionViewCell
- #pragma mark - Life Cycle
- - (id) initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if(self) {
- self.contentView.backgroundColor = [UIColor whiteColor];
- [self.activityIndicator startAnimating];
- _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
- self.scrollView.delegate = self;
- self.scrollView.showsVerticalScrollIndicator = NO;
- self.scrollView.showsHorizontalScrollIndicator = NO;
- // self.scrollView.maximumZoomScale = 1.0f;
- [self.contentView addSubview:self.scrollView];
- _portfolioImageView = [[RNImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
- self.portfolioImageView.clipsToBounds = YES;
- self.portfolioImageView.delegate = self;
- self.portfolioImageView.contentMode = UIViewContentModeScaleAspectFill;
- [self.scrollView addSubview:self.portfolioImageView];
- //AS TEMP CROPPED VIEW
- _croppedAreaView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
- self.croppedAreaView.clipsToBounds = YES;
- self.croppedAreaView.backgroundColor = [UIColor greenColor];
- self.croppedAreaView.alpha = 0.5f;
- [self.portfolioImageView addSubview:self.croppedAreaView];
- //END AS TEMP CROPPED VIEW
- UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
- tapTwice.numberOfTapsRequired = 2;
- [self.contentView addGestureRecognizer:tapTwice];
- _isLoading = YES;
- }
- return self;
- }
- - (void)prepareForReuse {
- [super prepareForReuse];
- self.portfolioImageView.image = nil;
- }
- #pragma mark - Delegate
- #pragma mark RNImageView
- - (void)RNImageViewDidFinishLoadImage:(RNImageView *)imageView {
- _isLoading = NO;
- [self refreshImageFromImageURL];
- [self.activityIndicator stopAnimating];
- }
- #pragma mark UIScrollView
- - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
- return self.portfolioImageView;
- }
- - (void)scrollViewDidZoom:(UIScrollView *)scrollView {
- [self centerScrollViewContents];
- }
- - (void)setImageWithURLData:(AvatarURLModel *)urlData {
- NSString *urlString = urlData.large;
- if(urlData.largeImageWidth == nil){
- self.imageURLWidth = 100.0f;
- }
- else {
- self.imageURLWidth = [urlData.largeImageWidth floatValue];
- }
- if(urlData.largeImageHeight == nil){
- self.imageURLHeight = 100.0f;
- }
- else {
- self.imageURLHeight = [urlData.largeImageHeight floatValue];
- }
- [self.portfolioImageView setImageWithURLString:urlString];
- }
- - (void)setImageWithURL:(NSString *)imageURL {
- NSString *urlString = imageURL;
- [self.portfolioImageView setImageWithURLString:urlString];
- }
- - (void)setImageWithImageData:(UIImage *)image {
- _isLoading = NO;
- [self.portfolioImageView setImage:image];
- [self refreshImageWithImage:image];
- }
- - (void)refreshImageWithImage:(UIImage *)image {
- self.portfolioImageView.frame = CGRectZero;
- //AS TEMP
- self.croppedAreaView.frame = CGRectZero;
- //END AS TEMP
- self.scrollView.contentSize = self.portfolioImageView.frame.size;
- self.scrollView.minimumZoomScale = 1.0f;
- self.scrollView.maximumZoomScale = 1.0f;
- self.scrollView.zoomScale = 1.0f;
- CGFloat photoZoomWidth = image.size.width;
- CGFloat photoZoomHeight = image.size.height;
- //AS TEMP
- //self.croppedAreaView.frame = CGRectMake(0.0f, ((image.size.height) - 205.0f) / 2.0f, image.size.width, 205.0f);
- //END AS TEMP
- //=========
- //AS TEMP
- 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);
- //END AS TEMP
- //AS TEMP topAreaView
- // _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 )];
- // self.topAreaView.backgroundColor = [UIColor redColor];
- // self.topAreaView.alpha = 0.5f;
- // [self.portfolioImageView addSubview:self.topAreaView];
- //END AS TEMP topAreaView
- //AS TEMP bottomAreaView
- // _bottomAreaView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, CGRectGetMaxY(self.portfolioImageView.frame) + 205.0f, photoZoomWidth, (CGRectGetHeight(self.portfolioImageView.frame) - CGRectGetHeight(self.croppedAreaView.frame)) / 2.0f )];
- // self.topAreaView.backgroundColor = [UIColor redColor];
- // self.topAreaView.alpha = 0.5f;
- // [self.portfolioImageView addSubview:self.topAreaView];
- //END AS TEMP bottomAreaView
- BOOL isPortrait = NO;
- if(photoZoomHeight > photoZoomWidth) {
- isPortrait = YES;
- }
- CGRect scrollViewFrame = self.scrollView.frame;
- if(isPortrait) {
- if(photoZoomHeight < CGRectGetHeight(scrollViewFrame)) {
- photoZoomWidth = (CGRectGetHeight(scrollViewFrame)/photoZoomHeight) * photoZoomWidth;
- photoZoomHeight = CGRectGetHeight(scrollViewFrame);
- }
- }
- else {
- if(photoZoomWidth < CGRectGetWidth(scrollViewFrame)) {
- photoZoomHeight = (CGRectGetWidth(scrollViewFrame)/photoZoomWidth) * photoZoomHeight;
- photoZoomWidth = CGRectGetWidth(scrollViewFrame);
- }
- }
- self.portfolioImageView.frame = CGRectMake(0.0f, 0.0f, photoZoomWidth, photoZoomHeight);
- // //AS TEMP
- // self.croppedAreaView.frame = CGRectMake(0.0f, ((photoZoomHeight) - (photoZoomHeight - 410.0f)) / 2.0f, image.size.width, photoZoomHeight - 410.0f);
- // //END AS TEMP
- self.scrollView.contentSize = self.portfolioImageView.frame.size;
- CGFloat scaleWidth = CGRectGetWidth(scrollViewFrame) / self.scrollView.contentSize.width;
- CGFloat scaleHeight = CGRectGetHeight(scrollViewFrame) / self.scrollView.contentSize.height;
- CGFloat minimumZoomScale = scaleWidth;
- if((self.scrollView.contentSize.width * scaleHeight) < CGRectGetWidth([UIScreen mainScreen].bounds)) {
- minimumZoomScale = scaleHeight;
- }
- if(!self.isLoading) {
- self.scrollView.minimumZoomScale = minimumZoomScale;
- self.scrollView.maximumZoomScale = 1.0f;
- self.scrollView.zoomScale = minimumZoomScale;
- }
- [self centerScrollViewContents];
- }
- - (void)refreshImageFromImageURL {
- self.portfolioImageView.frame = CGRectZero;
- //AS TEMP
- self.croppedAreaView.frame = CGRectZero;
- //END AS TEMP
- self.scrollView.contentSize = self.portfolioImageView.frame.size;
- self.scrollView.minimumZoomScale = 1.0f;
- self.scrollView.maximumZoomScale = 1.0f;
- self.scrollView.zoomScale = 1.0f;
- CGFloat photoZoomWidth = self.imageURLWidth;
- CGFloat photoZoomHeight = self.imageURLHeight;
- // //AS TEMP
- // self.croppedAreaView.frame = CGRectMake(0.0f, ((photoZoomHeight) - (photoZoomHeight - 410.0f)) / 2.0f, self.imageURLWidth, photoZoomHeight - 410.0f);
- // //END AS TEMP
- //AS TEMP
- self.croppedAreaView.frame = CGRectMake(0.0f, ((self.imageURLHeight) - ((self.imageURLWidth * 9.0f) / 16.0f)) / 2.0f, self.imageURLWidth, (self.imageURLWidth * 9.0f) / 16.0f);
- //END AS TEMP
- BOOL isPortrait = NO;
- if(photoZoomHeight > photoZoomWidth) {
- isPortrait = YES;
- }
- CGRect scrollViewFrame = self.scrollView.frame;
- if(isPortrait) {
- if(photoZoomHeight < CGRectGetHeight(scrollViewFrame)) {
- photoZoomWidth = (CGRectGetHeight(scrollViewFrame)/photoZoomHeight) * photoZoomWidth;
- photoZoomHeight = CGRectGetHeight(scrollViewFrame);
- }
- }
- else {
- if(photoZoomWidth < CGRectGetWidth(scrollViewFrame)) {
- photoZoomHeight = (CGRectGetWidth(scrollViewFrame)/photoZoomWidth) * photoZoomHeight;
- photoZoomWidth = CGRectGetWidth(scrollViewFrame);
- }
- }
- self.portfolioImageView.frame = CGRectMake(0.0f, 0.0f, photoZoomWidth, photoZoomHeight);
- //AS TEMP
- self.croppedAreaView.frame = CGRectMake(0.0f, ((photoZoomHeight) - (photoZoomHeight - 410.0f)) / 2.0f, self.imageURLWidth, photoZoomHeight - 410.0f);
- //END AS TEMP
- self.scrollView.contentSize = self.portfolioImageView.frame.size;
- CGFloat scaleWidth = CGRectGetWidth(scrollViewFrame) / self.scrollView.contentSize.width;
- CGFloat scaleHeight = CGRectGetHeight(scrollViewFrame) / self.scrollView.contentSize.height;
- CGFloat minimumZoomScale = scaleWidth;
- if((self.scrollView.contentSize.width * scaleHeight) < CGRectGetWidth([UIScreen mainScreen].bounds)) {
- minimumZoomScale = scaleHeight;
- }
- if(!self.isLoading) {
- self.scrollView.minimumZoomScale = minimumZoomScale;
- self.scrollView.maximumZoomScale = 1.0f;
- self.scrollView.zoomScale = minimumZoomScale;
- }
- [self centerScrollViewContents];
- }
- - (void)doubleTap:(UIGestureRecognizer *)gestureRecognizer {
- CGFloat maximumDoubleTapZoom = (self.scrollView.maximumZoomScale - self.scrollView.minimumZoomScale) / 2.0f;
- if(self.scrollView.zoomScale >= maximumDoubleTapZoom) {
- [self.scrollView setZoomScale:self.scrollView.minimumZoomScale animated:YES];
- return;
- }
- CGPoint pointInView = [gestureRecognizer locationInView:self.portfolioImageView];
- CGFloat newZoomScale = maximumDoubleTapZoom;
- CGSize scrollViewSize = self.scrollView.bounds.size;
- CGFloat width = scrollViewSize.width / newZoomScale;
- CGFloat height = scrollViewSize.height / newZoomScale;
- CGFloat x = pointInView.x - (width / 2.0f);
- CGFloat y = pointInView.y - (height / 2.0f);
- CGRect zoomRect = CGRectMake(x, y, width, height);
- [self.scrollView zoomToRect:zoomRect animated:YES];
- }
- - (void)centerScrollViewContents {
- CGSize boundsSize = self.scrollView.bounds.size;
- CGRect contentsFrame = self.portfolioImageView.frame;
- if (contentsFrame.size.width < boundsSize.width) {
- contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
- } else {
- contentsFrame.origin.x = 0.0f;
- }
- if (contentsFrame.size.height < boundsSize.height) {
- contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
- } else {
- contentsFrame.origin.y = 0.0f;
- }
- self.portfolioImageView.frame = contentsFrame;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement