Evernote'un iOS 7'de kullandığı esnek koleksiyon görünümünü yeniden oluşturmaya çalışıyorum ve çalışmaya gerçekten çok yakınım. İçerik ofseti y değeri koleksiyon görünüm sınırları dışında olduğunda düzen özelliği dönüşümlerini değiştiren özel bir koleksiyon görünümü akış düzeni oluşturmayı başardım. LayoutAttributesForElementsInRect yöntemindeki düzen niteliklerini değiştiriyorum ve kaydırma görünümünün altına vurduğunuzda alt hücrelerin kaybolacak olması dışında beklendiği gibi davranıyor. İçeriği daha fazla çekerseniz, daha fazla hücre kaybolabilir. Sanırım hücreler temelde kısıldı. En tepede olmaz ve her iki yerde de aynı davranışı görmeyi beklerdim. Akış düzeni uygulamam şu anki gibi görünüyor.iOS 7 üzerinde Evernote gibi bir UICollectionView oluşturma
@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
Ayrıca, kullanıcıların denemesi için projeyi sıkıştırdım. Özel koleksiyon görünümü düzenleri oluşturmak için oldukça yeni olduğum için herhangi bir yardım büyük takdir edilecektir. İşte buna link:
https://dl.dropboxusercontent.com/u/2975688/StackOverflow/stretchy_collection_view.zip
herkese teşekkürler!
Kodunuz, simülatörde beklendiği gibi çalışır. Kırpılmış hücre yok. – Mundi
Sadece test ettim ve iPhone Retina (3.5 inç) simülatörde iyi çalışıyor gibi görünüyor, ancak sorun iPhone Retina (4 inç) simülatörde gerçekleşiyor. – cnoon