ProductViewHeader.m 10.1 KB
Newer Older
1
//
2
//  ProductViewHeader.m
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
//  Mobile Buy SDK
//
//  Created by Shopify.
//  Copyright (c) 2015 Shopify Inc. All rights reserved.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//

27 28 29 30 31 32
#import "ProductViewHeader.h"
#import "AsyncImageView.h"
#import "GradientView.h"
#import "ProductImageCell.h"
#import "Theme+Additions.h"
#import "HeaderOverlayView.h"
33

34
@interface ProductViewHeader ()
35 36 37

@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UIView *bottomGradientContainerView;
38
@property (nonatomic, strong) GradientView *bottomGradientView;
39 40 41 42 43
@property (nonatomic, strong) NSLayoutConstraint *bottomGradientViewLayoutConstraintHeight;
@property (nonatomic, strong) NSLayoutConstraint *bottomGradientViewBottomContraint;

@end

44
@implementation ProductViewHeader
45

46
- (instancetype)initWithFrame:(CGRect)frame
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
{
	self = [super initWithFrame:frame];
	if (self) {
		self.backgroundColor = [UIColor clearColor];
		
		UICollectionViewFlowLayout *collectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
		collectionViewFlowLayout.sectionInset = UIEdgeInsetsZero;
		collectionViewFlowLayout.minimumLineSpacing = 0;
		collectionViewFlowLayout.itemSize = CGSizeMake(CGRectGetWidth(frame), CGRectGetHeight(frame));
		collectionViewFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
		_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:collectionViewFlowLayout];
		_collectionView.backgroundColor = [UIColor clearColor];
		_collectionView.showsHorizontalScrollIndicator = NO;
		_collectionView.pagingEnabled = YES;
		_collectionView.clipsToBounds = NO;
		_collectionView.translatesAutoresizingMaskIntoConstraints = NO;
63
		[_collectionView registerClass:[ProductImageCell class] forCellWithReuseIdentifier:@"Cell"];
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		[self addSubview:_collectionView];
		
		[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_collectionView]|"
																	 options:0
																	 metrics:nil
																	   views:NSDictionaryOfVariableBindings(_collectionView)]];
		
		[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_collectionView]|"
																	 options:0
																	 metrics:nil
																	   views:NSDictionaryOfVariableBindings(_collectionView)]];
		
		_bottomGradientContainerView = [[UIView alloc] init];
		_bottomGradientContainerView.backgroundColor = [UIColor clearColor];
		_bottomGradientContainerView.translatesAutoresizingMaskIntoConstraints = NO;
		_bottomGradientContainerView.clipsToBounds = YES;
		[self addSubview:_bottomGradientContainerView];
		
		[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_bottomGradientContainerView]|"
																	 options:0
																	 metrics:nil
																	   views:NSDictionaryOfVariableBindings(_bottomGradientContainerView)]];
		
		[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_bottomGradientContainerView]|"
																	 options:0
																	 metrics:nil
																	   views:NSDictionaryOfVariableBindings(_bottomGradientContainerView)]];
		
		_bottomGradientViewLayoutConstraintHeight = [NSLayoutConstraint constraintWithItem:_bottomGradientContainerView
																				 attribute:NSLayoutAttributeHeight
																				 relatedBy:NSLayoutRelationEqual
																					toItem:nil
																				 attribute:NSLayoutAttributeNotAnAttribute
																				multiplier:1.0
																				  constant:kBuyBottomGradientHeightWithoutPageControl];
		[self addConstraint:_bottomGradientViewLayoutConstraintHeight];
		
101
		_bottomGradientView = [[GradientView alloc] init];
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
		_bottomGradientView.userInteractionEnabled = NO;
		_bottomGradientView.topColor = [UIColor clearColor];
		_bottomGradientView.bottomColor = [UIColor colorWithWhite:0 alpha:0.05f];
		_bottomGradientView.translatesAutoresizingMaskIntoConstraints = NO;
		[_bottomGradientContainerView addSubview:_bottomGradientView];
		
		[_bottomGradientContainerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_bottomGradientView]|"
																							 options:0
																							 metrics:nil
																							   views:NSDictionaryOfVariableBindings(_bottomGradientView)]];
		
		[_bottomGradientContainerView addConstraint:[NSLayoutConstraint constraintWithItem:_bottomGradientView
																				 attribute:NSLayoutAttributeHeight
																				 relatedBy:NSLayoutRelationEqual
																					toItem:_bottomGradientContainerView
																				 attribute:NSLayoutAttributeHeight
																				multiplier:1.0
																				  constant:0.0]];
		
		_bottomGradientViewBottomContraint = [NSLayoutConstraint constraintWithItem:_bottomGradientView
																		  attribute:NSLayoutAttributeBottom
																		  relatedBy:NSLayoutRelationEqual
																			 toItem:_bottomGradientContainerView
																		  attribute:NSLayoutAttributeBottom
																		 multiplier:1.0
																		   constant:0.0];
		[_bottomGradientContainerView addConstraint:_bottomGradientViewBottomContraint];
		
		_pageControl = [[UIPageControl alloc] init];
		_pageControl.hidesForSinglePage = YES;
		_pageControl.translatesAutoresizingMaskIntoConstraints = NO;
		_pageControl.userInteractionEnabled = NO;
		[_bottomGradientView addSubview:_pageControl];
		
		[_bottomGradientView addConstraint:[NSLayoutConstraint constraintWithItem:_pageControl
																   attribute:NSLayoutAttributeBottom
																   relatedBy:NSLayoutRelationEqual
																	  toItem:_bottomGradientView
																   attribute:NSLayoutAttributeBottom
																  multiplier:1.0
																	constant:0.0]];
		
		[_bottomGradientView addConstraint:[NSLayoutConstraint constraintWithItem:_pageControl
																		attribute:NSLayoutAttributeWidth
																		relatedBy:NSLayoutRelationEqual
																		   toItem:_bottomGradientView
																		attribute:NSLayoutAttributeWidth
																	   multiplier:1.0
																		 constant:0.0]];
		
		[_bottomGradientView addConstraint:[NSLayoutConstraint constraintWithItem:_pageControl
																		attribute:NSLayoutAttributeHeight
																		relatedBy:NSLayoutRelationEqual
																		   toItem:nil
																		attribute:NSLayoutAttributeNotAnAttribute
																	   multiplier:1.0
																		 constant:kBuyPageControlHeight]];
		
160
		_productViewHeaderOverlay = [[HeaderOverlayView alloc] init];
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
		_productViewHeaderOverlay.translatesAutoresizingMaskIntoConstraints = NO;
		[self addSubview:_productViewHeaderOverlay];
		[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_productViewHeaderOverlay]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_productViewHeaderOverlay)]];
		[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_productViewHeaderOverlay]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_productViewHeaderOverlay)]];
	}
	return self;
}

- (void)setNumberOfPages:(NSInteger)numberOfPages
{
	self.pageControl.numberOfPages = numberOfPages;
	if (self.pageControl.numberOfPages == 0) {
		self.bottomGradientViewLayoutConstraintHeight.constant = kBuyBottomGradientHeightWithoutPageControl;
		self.bottomGradientView.bottomColor = [UIColor colorWithWhite:0 alpha:0.05f];
	} else {
		self.bottomGradientViewLayoutConstraintHeight.constant = kBuyBottomGradientHeightWithPageControl;
		self.bottomGradientView.bottomColor = [UIColor colorWithWhite:0 alpha:0.15f];
	}
}

- (void)setCurrentPage:(NSInteger)currentPage
{
	self.pageControl.currentPage = currentPage;
}

- (CGFloat)imageHeightWithScrollViewDidScroll:(UIScrollView *)scrollView
{
	self.bottomGradientViewBottomContraint.constant = MAX(scrollView.contentOffset.y / 4, 0);
	CGRect visibleRect = (CGRect){.origin = self.collectionView.contentOffset, .size = self.collectionView.bounds.size};
	CGPoint visiblePoint = CGPointMake(CGRectGetMidX(visibleRect), CGRectGetMidY(visibleRect));
	NSIndexPath *visibleIndexPath = [self.collectionView indexPathForItemAtPoint:visiblePoint];
192
	ProductImageCell *cell = (ProductImageCell*)[self.collectionView cellForItemAtIndexPath:visibleIndexPath];
193 194 195 196 197 198 199 200 201 202 203 204 205
	if (cell) {
		[cell setContentOffset:scrollView.contentOffset];
		return cell.productImageViewConstraintHeight.constant;
	} else {
		cell = [self.collectionView.visibleCells firstObject];
		return cell.productImageViewConstraintHeight.constant;
	}
}

- (void)setImageForSelectedVariant:(BUYProductVariant*)productVariant withImages:(NSArray*)images
{
	[self setNumberOfPages:[images count]];
	if (CGSizeEqualToSize(self.collectionView.contentSize, CGSizeZero) == NO) {
206
		[images enumerateObjectsUsingBlock:^(BUYImageLink *image, NSUInteger i, BOOL *stop) {
207 208 209 210 211 212 213 214 215 216 217 218 219
			for (NSNumber *variantId in image.variantIds) {
				if ([variantId isEqualToNumber:productVariant.identifier]) {
					[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
					self.pageControl.currentPage = i;
					*stop = YES;
					break;
				}
			}
		}];
	}
}

@end