I have a UICollectionView
with items. I want items in center be full color, and items closer to vertical borders of collection view to slowly fade.
I came up with my custom implementation of layoutAttributesForElementsInRect
but it doesn't seem to work as intended.
First, it's getting rarely called. Once at startup and sometimes later, usually when I'm near collectionView
end.
Second, I fail to get normalized distance, it goes from -2 to 6, and I just want 0 to 1.
What exactly am I failing here? I take the visible rect and calculate distance from cell center to its edges and then normalize it.
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *attribs = [super layoutAttributesForElementsInRect:rect];
CGRect visibleRect;
visibleRect.origin = self.collectionView.contentOffset;
visibleRect.size = self.collectionView.bounds.size;
for (UICollectionViewLayoutAttributes *attributes in attribs) {
if (CGRectIntersectsRect(attributes.frame, rect)) {
CGFloat distanceFromCenter = CGRectGetMidY(visibleRect) - attributes.center.y;
CGFloat normalizedDistance = distanceFromCenter / (visibleRect.size.height / 2);
attributes.alpha = 1 - normalizedDistance;
}
}
return attribs;
}
Update:
Here is what I want:
When the top line of dates (22-28) approaches the edge of collection view, I want them to slowly fade out. Same goes about the bottom row. As you can see dates 1 and 2 are already not visible since they were light grey color. Dates 1-31 are black because I highlight the month that occupies the majority of view now.
I do this via scroll view delegate method after each scroll now, but this is very ugly code.
I want to do the same in the layout attributes method.