Skip to content

Instantly share code, notes, and snippets.

@beccadax
Created July 14, 2013 03:26
Show Gist options
  • Select an option

  • Save beccadax/5993071 to your computer and use it in GitHub Desktop.

Select an option

Save beccadax/5993071 to your computer and use it in GitHub Desktop.

Revisions

  1. beccadax created this gist Jul 14, 2013.
    13 changes: 13 additions & 0 deletions FDRCollectionViewCell.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    //
    // FDRCollectionViewCell.h
    //
    //
    // Created by Brent Royal-Gordon on 7/10/13.
    //
    //

    #import <UIKit/UIKit.h>

    @interface FDRCollectionViewCell : UICollectionViewCell

    @end
    82 changes: 82 additions & 0 deletions FDRCollectionViewCell.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    //
    // FDRCollectionViewCell.m
    //
    //
    // Created by Brent Royal-Gordon on 7/10/13.
    //
    //

    #import "FDRCollectionViewCell.h"
    #import "FDRCollectionViewFlowLayout.h"

    @interface FDRCollectionViewCellBackgroundView : UIView

    @property (assign, nonatomic) BOOL rightBorderHidden;
    @property (assign, nonatomic) BOOL bottomBorderHidden;

    @end

    @implementation FDRCollectionViewCell

    - (void)awakeFromNib {
    [super awakeFromNib];

    self.backgroundView = [FDRCollectionViewCellBackgroundView new];
    self.backgroundView.backgroundColor = self.backgroundColor;
    self.backgroundView.opaque = self.opaque;

    self.selectedBackgroundView = [UIView new];
    self.selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.03];
    self.selectedBackgroundView.opaque = NO;
    }

    - (void)setBackgroundColor:(UIColor *)backgroundColor {
    super.backgroundColor = backgroundColor;

    self.backgroundView.backgroundColor = backgroundColor;
    }

    - (void)setOpaque:(BOOL)opaque {
    super.opaque = opaque;

    self.backgroundView.opaque = opaque;
    }

    - (void)applyLayoutAttributes:(FDRCollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];

    FDRCollectionViewCellBackgroundView * background = (id)self.backgroundView;
    background.bottomBorderHidden = layoutAttributes.inLastRowOfSection && !layoutAttributes.inLastSection;
    background.rightBorderHidden = layoutAttributes.againstRightEdge;
    }

    @end

    @implementation FDRCollectionViewCellBackgroundView

    - (void)setBottomBorderHidden:(BOOL)bottomBorderHidden {
    _bottomBorderHidden = bottomBorderHidden;
    [self setNeedsDisplay];
    }

    - (void)setRightBorderHidden:(BOOL)rightBorderHidden {
    _rightBorderHidden = rightBorderHidden;
    [self setNeedsDisplay];
    }

    - (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    [[UIColor colorWithWhite:0.74 alpha:1.0] set];

    CGFloat thickness = 1 / self.contentScaleFactor;

    if(!self.rightBorderHidden) {
    UIRectFill(CGRectMake(CGRectGetMaxX(self.bounds) - thickness, 0, thickness, CGRectGetHeight(self.bounds)));
    }
    if(!self.bottomBorderHidden) {
    UIRectFill(CGRectMake(0, CGRectGetMaxY(self.bounds) - thickness, CGRectGetWidth(self.bounds), thickness));
    }
    }

    @end
    22 changes: 22 additions & 0 deletions FDRCollectionViewFlowLayout.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    //
    // FDRCollectionViewFlowLayout.h
    // Feeder
    //
    // Created by Brent Royal-Gordon on 7/11/13.
    // Copyright (c) 2013 Architechies. All rights reserved.
    //

    #import <UIKit/UIKit.h>

    @interface FDRCollectionViewFlowLayout : UICollectionViewFlowLayout

    @end

    @interface FDRCollectionViewLayoutAttributes : UICollectionViewLayoutAttributes

    @property (assign, nonatomic, getter = isInLastRowOfSection) BOOL inLastRowOfSection;
    @property (assign, nonatomic, getter = isInLastSection) BOOL inLastSection;

    @property (assign, nonatomic, getter = isAgainstRightEdge) BOOL againstRightEdge;

    @end
    57 changes: 57 additions & 0 deletions FDRCollectionViewFlowLayout.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    //
    // FDRCollectionViewFlowLayout.m
    // Feeder
    //
    // Created by Brent Royal-Gordon on 7/11/13.
    // Copyright (c) 2013 Architechies. All rights reserved.
    //

    #import "FDRCollectionViewFlowLayout.h"

    @implementation FDRCollectionViewFlowLayout

    + (Class)layoutAttributesClass {
    return [FDRCollectionViewLayoutAttributes class];
    }

    - (void)intuitSectioningAttributes:(FDRCollectionViewLayoutAttributes*)attributes {
    NSIndexPath * lastIndexPath = [NSIndexPath indexPathForItem:[self.collectionView numberOfItemsInSection:attributes.indexPath.section] - 1 inSection:attributes.indexPath.section];
    attributes.inLastRowOfSection = [lastIndexPath isEqual:attributes.indexPath] || attributes.center.y == [super layoutAttributesForItemAtIndexPath:lastIndexPath].center.y;

    attributes.inLastSection = attributes.indexPath.section == self.collectionView.numberOfSections - 1;

    attributes.againstRightEdge = CGRectGetMaxX(attributes.frame) == CGRectGetMaxX(self.collectionView.bounds);
    }

    - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    FDRCollectionViewLayoutAttributes * attributes = (id)[super layoutAttributesForItemAtIndexPath:indexPath];

    [self intuitSectioningAttributes:attributes];

    return attributes;
    }

    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSArray * attributesArray = [super layoutAttributesForElementsInRect:rect];
    for(FDRCollectionViewLayoutAttributes * attrs in attributesArray) {
    [self intuitSectioningAttributes:attrs];
    }
    return attributesArray;
    }

    @end

    @implementation FDRCollectionViewLayoutAttributes

    - (id)copyWithZone:(NSZone *)zone {
    FDRCollectionViewLayoutAttributes * copy = [super copyWithZone:zone];

    copy.inLastRowOfSection = self.inLastRowOfSection;
    copy.inLastSection = self.inLastSection;

    copy.againstRightEdge = self.againstRightEdge;

    return copy;
    }

    @end
    15 changes: 15 additions & 0 deletions FDRSectionHeaderView.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    //
    // FDRSectionHeaderView.h
    // Feeder
    //
    // Created by Brent Royal-Gordon on 7/11/13.
    // Copyright (c) 2013 Architechies. All rights reserved.
    //

    #import <UIKit/UIKit.h>

    @interface FDRSectionHeaderView : UICollectionReusableView

    @property (weak, nonatomic) IBOutlet UILabel * titleLabel;

    @end
    24 changes: 24 additions & 0 deletions FDRSectionHeaderView.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    //
    // FDRSectionHeaderView.m
    // Feeder
    //
    // Created by Brent Royal-Gordon on 7/11/13.
    // Copyright (c) 2013 Architechies. All rights reserved.
    //

    #import "FDRSectionHeaderView.h"

    @implementation FDRSectionHeaderView

    - (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    [[UIColor colorWithWhite:0.74 alpha:1.0] set];

    CGFloat thickness = 1 / self.contentScaleFactor;

    UIRectFill(CGRectMake(0, 0, CGRectGetWidth(self.bounds), thickness));
    UIRectFill(CGRectMake(0, CGRectGetMaxY(self.bounds) - thickness, CGRectGetWidth(self.bounds), thickness));
    }

    @end