Skip to content

Instantly share code, notes, and snippets.

@cjazz
Created November 1, 2016 19:22
Show Gist options
  • Save cjazz/46f1c39d1d3dde9ccfcd181609ea250c to your computer and use it in GitHub Desktop.
Save cjazz/46f1c39d1d3dde9ccfcd181609ea250c to your computer and use it in GitHub Desktop.
UICollectionView - demoing 2 horizontal UICollectionViews with a top profile image
//
// testCollectionViC.m
//
// Created by Adam Chin
// Copyright © 2016 HushBox. All rights reserved.
//
#import "testCollectionVC.h"
#import "CoreDataController.h"
#import "CoreDataHelper.h"
#import <CoreData/CoreData.h>
#import "Guests.h"
#import "GuestWishes.h"
#import "GuestBoxSwagCell.h"
#import "GuestBoxSwagPDFCell.h"
#define ImageHeaderHeightRegular 600
#define ImageHeaderHeightCompact 300
#define ContentContainerHeightRegular 1500
#define ContentContainerHeightCompact 1000
@interface testCollectionVC () <UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate>
{
CGFloat imageViewYCoordinate;
CGFloat belowImageViewYCoordinate;
}
@property (strong, nonatomic) IBOutlet UICollectionView *swagCollectionView;
@property (strong, nonatomic) IBOutlet UICollectionView *pdfCollectionView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *gWishArray;
@property (nonatomic, strong) NSMutableArray *gWishPDFArray;
@property (strong, nonatomic) Guests *guest;
@property (strong, nonatomic) GuestWishes *currentObject;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imageHeaderHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentContainerHeight;
@end
@implementation testCollectionViC
static NSString * const reuseIdentifier = @"Cell";
// MARK: View Delegate Methods
- (void)viewDidLoad {
[super viewDidLoad];
self.swagCollectionView.delegate = self;
self.swagCollectionView.dataSource = self;
self.pdfCollectionView.delegate = self;
self.pdfCollectionView.dataSource = self;
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
}
// Simply fade the image out and in based on scroll
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// Fade header image up with scrolling
// don't respond to the 2 collectionView scrollers
// if (![scrollView isKindOfClass:[UICollectionView class]])
// {
// // calculate distance moved verticlaly into imageViewYcoord
// float distMoved = scrollView.contentOffset.y;
// imageViewYCoordinate = distMoved * 2;
// self.headerImageView.alpha = 1 - imageViewYCoordinate/self.headerImageView.frame.size.height;
//
// }
}
// Singleline with two colors, pass in the string which will be the Normal Color, the rest in the highlight color
-(NSMutableAttributedString*)makeAttributedStringFromLabel:(NSString*)label andString:(NSString*)strVal
{
UIColor *normalColor = SGAquaGreen;
UIColor *highlightColor = [UIColor blackColor];
// UIFont *font = [UIFont systemFontOfSize:12.0];
NSDictionary *normalAttributes = @{NSForegroundColorAttributeName:normalColor};
NSDictionary *highlightAttributes = @{NSForegroundColorAttributeName:highlightColor};
NSAttributedString *normalText = [[NSAttributedString alloc] initWithString:label attributes:normalAttributes];
NSAttributedString *highlightedText = [[NSAttributedString alloc] initWithString:strVal attributes:highlightAttributes];
NSMutableAttributedString *finalAttributedString = [[NSMutableAttributedString alloc] initWithAttributedString:normalText];
[finalAttributedString appendAttributedString:highlightedText];
return finalAttributedString;
}
// MARK: Get & Set Data
-(void)getInitalViewData
{
self.managedObjectContext = [[CoreDataController sharedInstance] newManagedObjectContext];
self.gWishArray = [self setUpDataForCollections:@"app"];
self.gWishPDFArray = [self setUpDataForCollections:@"web"];
}
// Set up predicate for sources, can call each source to set up each array:
-(NSMutableArray *)setUpDataForCollections:(NSString*)source
{
NSPredicate *profilePredicate = [NSPredicate predicateWithFormat:@"guests == %@", self.guest];
NSPredicate *sourcePredicate = [NSPredicate predicateWithFormat:@"source == %@", source];
NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[profilePredicate, sourcePredicate]];
NSMutableArray *returnArray = [CoreDataHelper searchObjectsForEntity:@"GuestWishes"
withPredicate:finalPredicate
andSortKey:@"displayOrder"
andSortAscending:YES
andContext:self.managedObjectContext];
if (returnArray) return returnArray;
else return nil;
return nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// MARK: Trait collection changes
- (void) traitCollectionDidChange: (nullable UITraitCollection *) previousTraitCollection
{
/* Check the horizontal trait environment and adjust the layout accordingly. Only deactivate active constraints. */
if ([self.traitCollection containsTraitsInCollection: [UITraitCollection traitCollectionWithHorizontalSizeClass: UIUserInterfaceSizeClassCompact]])
{
self.imageHeaderHeight.constant = ImageHeaderHeightCompact;
self.contentContainerHeight.constant = ContentContainerHeightCompact;
}
else
{
self.imageHeaderHeight.constant = ImageHeaderHeightRegular;
self.contentContainerHeight.constant = ContentContainerHeightRegular;
}
}
// MARK: UICollectionView Delegate methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if ([collectionView isEqual:self.swagCollectionView])
{
return [self.gWishArray count];
}
else if ([collectionView isEqual:self.pdfCollectionView])
{
return [self.gWishPDFArray count];
}
else return 0;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if ([collectionView isEqual:self.swagCollectionView])
{
[self performSegueWithIdentifier:@"guestBoxSwagDetail" sender:[self.gWishArray objectAtIndex:indexPath.item]];
}
else if ([collectionView isEqual:self.pdfCollectionView])
{
[self performSegueWithIdentifier:@"pdfDetail" sender:[self.gWishPDFArray objectAtIndex:indexPath.item]];
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *gbSwagCell = nil;
if ([collectionView isEqual:self.swagCollectionView])
{
static NSString *identifier = @"cell";
GuestBoxSwagCell *gbSwagCell = (GuestBoxSwagCell *)[self.swagCollectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
if (gbSwagCell == nil)
{
gbSwagCell = [[GuestBoxSwagCell alloc] init];
}
GuestWishes *swag = (GuestWishes*)[self.gWishArray objectAtIndex:indexPath.item];
UIImage *swagImage = [UIImage imageWithData:[swag largeImageData]];
gbSwagCell.layer.cornerRadius = 5.0;
gbSwagCell.layer.masksToBounds = YES;
gbSwagCell.clipsToBounds = YES;
gbSwagCell.swagImageView.image = swagImage;
gbSwagCell.labelName.text = swag.name;
gbSwagCell.labelName.textColor = [UIColor whiteColor];
gbSwagCell.labelName.backgroundColor = [UIColor clearColor];
gbSwagCell.labelContainerView.backgroundColor = HBBlackAlphaBackground;
gbSwagCell.labelName.adjustsFontSizeToFitWidth = true;
gbSwagCell.backgroundColor = HBBlackAlphaBackground;
return gbSwagCell;
}
else
{
if ([collectionView isEqual:self.pdfCollectionView])
{
static NSString *identifier = @"pdfCell";
GuestBoxSwagPDFCell *gbSwagCell = (GuestBoxSwagPDFCell *)[self.pdfCollectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
if (gbSwagCell == nil)
{
gbSwagCell = [[GuestBoxSwagPDFCell alloc] init];
}
GuestWishes *swag = (GuestWishes*)[self.gWishPDFArray objectAtIndex:indexPath.item];
UIImage *swagImage = [UIImage imageWithData:[swag largeImageData]];
gbSwagCell.layer.cornerRadius = 5.0;
gbSwagCell.layer.masksToBounds = YES;
gbSwagCell.clipsToBounds = YES;
gbSwagCell.swagImageView.image = swagImage;
gbSwagCell.labelName.text = swag.name;
gbSwagCell.labelName.textColor= [UIColor whiteColor];
gbSwagCell.labelName.backgroundColor = [UIColor clearColor];
gbSwagCell.pdfLabelContainerView.backgroundColor = [UIColor whiteColor];
gbSwagCell.pdfLogo.image = [[UIImage imageNamed:@"pdf_icon2"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
gbSwagCell.pdfLogo.tintColor = [UIColor lightGrayColor];
gbSwagCell.contentMode = UIViewContentModeScaleAspectFit;
return gbSwagCell;
}
}
return gbSwagCell;
}
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return NO;
}
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
}
@end
@cjazz
Copy link
Author

cjazz commented Nov 1, 2016

guestboxcollection

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment