Skip to content

Instantly share code, notes, and snippets.

@syshen
Last active December 11, 2015 03:39
Show Gist options
  • Select an option

  • Save syshen/4539533 to your computer and use it in GitHub Desktop.

Select an option

Save syshen/4539533 to your computer and use it in GitHub Desktop.
@interface SSLayoutAttributes : UICollectionViewLayoutAttributes
@property (nonatomic, strong) CAGradientLayer *gradient; // a new attribute
@end
@implementation SSLayoutAttributes
- (id) copyWithZone:(NSZone *)zone {
SSLayoutAttributes *layoutAttribue = [super copyWithZone:zone];
layoutAttribue.gradient = _gradient;
return layoutAttribue;
}
@end
@interface SSFlowLayout : UICollectionViewFlowLayout
@end
@implementation SSFlowLayout
+(Class) layoutAttributesClass {
return [SSLayoutAttributes class];
}
- (NSArray*) layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *array = [super layoutAttributesForElementsInRect:rect];
for (SSLayoutAttributes *attr in array) {
if (CGRectIntersectsRect(rect, attr.frame)) {
[self applyAttributes:attr];
}
}
return array;
}
- (UICollectionViewLayoutAttributes*) layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
SSLayoutAttributes *attr = (SSLayoutAttributes*)[super layoutAttributesForItemAtIndexPath:indexPath];
[self applyAttributes:(SSLayoutAttributes*)attr];
return attr;
}
- (void) applyAttributes:(SSLayoutAttributes *)attributes {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = (CGRect) {CGPointZero, attributes.frame.size};
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:1.0f alpha:0.0f] CGColor],
(id)[[UIColor colorWithWhite:0.9 alpha:0.3f] CGColor],
(id)[[UIColor colorWithWhite:0.7 alpha:0.5f] CGColor],
(id)[[UIColor colorWithWhite:0 alpha:0.7f] CGColor],
(id)[[UIColor colorWithWhite:0 alpha:0.95f] CGColor], nil];
attributes.gradient = gradient;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment