Ich habe versucht, die Ansicht der dehnbaren Sammlung zu erstellen, die Evernote in iOS 7 verwendet, und ich bin wirklich nah daran, es funktioniert zu haben. Es ist mir gelungen, ein benutzerdefiniertes Layout für den Sammlungsansichtsfluss zu erstellen, das die Transformation des Layoutattributs ändert, wenn der Wert für den Inhaltsversatz y außerhalb der Grenzen der Sammlungsansicht liegt. Ich ändere die Layoutattribute in der layoutAttributesForElementsInRect-Methode und verhält sich wie erwartet, außer dass die unteren Zellen verschwinden können, wenn Sie am unteren Rand der Bildlaufansicht klicken. Je weiter Sie den Inhalt verschieben, desto mehr Zellen können verschwinden. Ich denke, die Zellen werden grundsätzlich abgeschnitten. Es passiert nicht an der Spitze und ich würde erwarten, das gleiche Verhalten an beiden Orten zu sehen. So sieht meine Flow-Layout-Implementierung jetzt aus.Erstellen einer dehnbaren UICollectionView wie Evernote auf iOS 7
@implementation CNStretchyCollectionViewFlowLayout
{
BOOL _transformsNeedReset;
CGFloat _scrollResistanceDenominator;
}
- (id)init
{
self = [super init];
if (self)
{
// Set up the flow layout parameters
self.minimumInteritemSpacing = 10;
self.minimumLineSpacing = 10;
self.itemSize = CGSizeMake(320, 44);
self.sectionInset = UIEdgeInsetsMake(10, 0, 10, 0);
// Set up ivars
_transformsNeedReset = NO;
_scrollResistanceDenominator = 800.0f;
}
return self;
}
- (void)prepareLayout
{
[super prepareLayout];
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
// Set up the default attributes using the parent implementation
NSArray *items = [super layoutAttributesForElementsInRect:rect];
// Compute whether we need to adjust the transforms on the cells
CGFloat collectionViewHeight = self.collectionViewContentSize.height;
CGFloat topOffset = 0.0f;
CGFloat bottomOffset = collectionViewHeight - self.collectionView.frame.size.height;
CGFloat yPosition = self.collectionView.contentOffset.y;
// Update the transforms if necessary
if (yPosition < topOffset)
{
// Compute the stretch delta
CGFloat stretchDelta = topOffset - yPosition;
NSLog(@"Stretching Top by: %f", stretchDelta);
// Iterate through all the visible items for the new bounds and update the transform
for (UICollectionViewLayoutAttributes *item in items)
{
CGFloat distanceFromTop = item.center.y;
CGFloat scrollResistance = distanceFromTop/800.0f;
item.transform = CGAffineTransformMakeTranslation(0, -stretchDelta + (stretchDelta * scrollResistance));
}
// Update the ivar for requiring a reset
_transformsNeedReset = YES;
}
else if (yPosition > bottomOffset)
{
// Compute the stretch delta
CGFloat stretchDelta = yPosition - bottomOffset;
NSLog(@"Stretching bottom by: %f", stretchDelta);
// Iterate through all the visible items for the new bounds and update the transform
for (UICollectionViewLayoutAttributes *item in items)
{
CGFloat distanceFromBottom = collectionViewHeight - item.center.y;
CGFloat scrollResistance = distanceFromBottom/800.0f;
item.transform = CGAffineTransformMakeTranslation(0, stretchDelta + (-stretchDelta * scrollResistance));
}
// Update the ivar for requiring a reset
_transformsNeedReset = YES;
}
else if (_transformsNeedReset)
{
NSLog(@"Resetting transforms");
_transformsNeedReset = NO;
for (UICollectionViewLayoutAttributes *item in items)
item.transform = CGAffineTransformIdentity;
}
return items;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
// Compute whether we need to adjust the transforms on the cells
CGFloat collectionViewHeight = self.collectionViewContentSize.height;
CGFloat topOffset = 0.0f;
CGFloat bottomOffset = collectionViewHeight - self.collectionView.frame.size.height;
CGFloat yPosition = self.collectionView.contentOffset.y;
// Handle cases where the layout needs to be rebuilt
if (yPosition < topOffset)
return YES;
else if (yPosition > bottomOffset)
return YES;
else if (_transformsNeedReset)
return YES;
return NO;
}
@end
Ich zippte auch das Projekt für Leute zum Ausprobieren. Jede Hilfe würde sehr geschätzt werden, da ich ziemlich neu beim Erstellen von benutzerdefinierten Auflistungsansichtslayouts bin. Hier ist der Link zu ihm:
https://dl.dropboxusercontent.com/u/2975688/StackOverflow/stretchy_collection_view.zip
Danke an alle!
Ihr Code funktioniert wie erwartet im Simulator. Keine abgeschnittenen Zellen. – Mundi
Ich habe gerade getestet und es scheint gut zu funktionieren in der iPhone Retina (3,5 Zoll) -Simulator, aber das Problem tritt mit dem iPhone Retina (4 Zoll) -Simulator. – cnoon