2013-04-06 9 views
24

私のプロジェクトの1つにブックシェルフを作成します。次のように基本的な要件は次のとおりUICollectionViewを使用してブックシェルフを作成する

    のiBookの本棚に類似
  • サポートするアイテムを削除して挿入サポートウェルIOSデバイスのすべての種類(異なる解像度)
  • をサポート
  • ウェルの両方の向きをサポート長押しジェスチャによるアイテムの並び替え
  • 最初の行をプルダウンすると隠しロゴが表示される

UICollectionViewは、私に起こった最初のオプションです。グリッドセルを簡単にサポートします。だから私はそれをGoogleで検索し、いくつかの非常に便利なチュートリアルが見つかりました:(なぜなら私はアールを選択したグラフィックの色矛盾の問題を無視してください:

Bryan Hansen's UICollectionView custom layout tutorial

Mark Pospesel's How to Add a Decoration View to a UICollectionView

LXReorderableCollectionViewFlowLayout

そして、ここに結果であり、まだ完全ではありません)

enter image description here

私がやったこと

  • UICollectionFlowLayout
  • から を継承した(目的の並べ替えのために)LXReorderableCollectionViewFlowLayoutから継承したクラスを作成することにより、カスタムレイアウトを作成し

    1. ロゴを示すための装飾ビューを表示するための装飾のビューを追加しました追加しましたbookshelfs

    しかし、私はいくつかの問題に遭遇した:

    1.項目が1つの画面に

    を示すことができる場合、私はその後、私はその後、私は今、スクロールすることができますcontentsizeが大きく

    - (CGSize)collectionViewContentSize 
    { 
        return CGSizeMake(self.collectionView.bounds.size.width,  self.collectionView.bounds.size.height+100); 
    } 
    

    を作るために、次のコードを追加して、全くスクロールすることはできません。ここで私は、最初の行をプルダウン:

    enter image description here

    あなたはロゴの装飾ビューが動作している見ることができます。

    2.しかし、私は最後の行をプルアップするとき、私は問題の第2のセットを得た:

    enter image description here

    あなたは装飾ビューが緑色のボックス部分に追加されません見ることができます。

    3。本棚のデコレーションビューの背景が暗くなり、暗くなります。

    - (void)prepareLayout 
    { 
        // call super so flow layout can do all the math for cells, headers, and footers 
        [super prepareLayout]; 
    
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; 
        NSMutableDictionary *shelfLayoutInfo = [NSMutableDictionary dictionary]; 
    
        // decoration view - emblem 
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0]; 
        UICollectionViewLayoutAttributes *emblemAttributes = 
         [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:[EmblemView kind] 
          withIndexPath:indexPath]; 
        emblemAttributes.frame = [self frameForEmblem:YES]; 
        dictionary[[EmblemView kind]] = @{indexPath: emblemAttributes}; 
    
        // Calculate where shelves go in a vertical layout 
        int sectionCount = [self.collectionView numberOfSections]; 
    
        CGFloat y = 0; 
        CGFloat availableWidth = self.collectionViewContentSize.width - (self.sectionInset.left + self.sectionInset.right); 
        int itemsAcross = floorf((availableWidth + self.minimumInteritemSpacing)/(self.itemSize.width + self.minimumInteritemSpacing)); 
    
        for (int section = 0; section < sectionCount; section++) 
        { 
         y += self.headerReferenceSize.height; 
         //y += self.sectionInset.top; 
    
         int itemCount = [self.collectionView numberOfItemsInSection:section]; 
         int rows = ceilf(itemCount/(float)itemsAcross)+1; // add 2 more empty row which doesn't have any data 
         for (int row = 0; row < rows; row++) 
         { 
          indexPath = [NSIndexPath indexPathForItem:row inSection:section]; 
          shelfLayoutInfo[indexPath] = [NSValue valueWithCGRect:CGRectMake(0,y, self.collectionViewContentSize.width, self.itemSize.height + DECORATION_HEIGHT)]; 
          y += self.itemSize.height; 
    
          if (row < rows - 1) 
           y += self.minimumLineSpacing; 
         } 
    
         y += self.sectionInset.bottom; 
         y += self.footerReferenceSize.height; 
        } 
    
        dictionary[[ShelfView kind]] = shelfLayoutInfo; 
    
        self.shelfLayoutInfo = dictionary; 
    } 
    
    
    - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 
    { 
        NSArray *attributesArrayInRect = [super layoutAttributesForElementsInRect:rect]; 
    
        // cell layout info 
        for (BookShelfLayoutAttributes *attribs in attributesArrayInRect) 
        { 
    
         attribs.zIndex = 1; 
         CATransform3D t = CATransform3DIdentity; 
         t = CATransform3DTranslate(t, 0, 0, 40); 
         attribs.transform3D = CATransform3DRotate(t, 15 * M_PI/180, 1, 0, 0); 
        } 
    
        // Add our decoration views (shelves) 
        NSMutableDictionary* shelfDictionary = self.shelfLayoutInfo[[ShelfView kind]]; 
        NSMutableArray *newArray = [attributesArrayInRect mutableCopy]; 
    
        [shelfDictionary enumerateKeysAndObjectsUsingBlock:^(id key, NSValue* obj, BOOL *stop) { 
    
         if (CGRectIntersectsRect([obj CGRectValue], rect)) 
         { 
          UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:[ShelfView kind] withIndexPath:key]; 
          attributes.frame = [obj CGRectValue]; 
    
          NSLog(@"decorationView rect = %@",NSStringFromCGRect(attributes.frame)); 
          attributes.zIndex = 0; 
          //attributes.alpha = 0.5; // screenshots 
          [newArray addObject:attributes]; 
         } 
        }]; 
    
        attributesArrayInRect = [NSArray arrayWithArray:newArray]; 
    
        NSMutableDictionary* emblemDictionary = self.shelfLayoutInfo[[EmblemView kind]]; 
        NSMutableArray *newArray2 = [attributesArrayInRect mutableCopy]; 
        [emblemDictionary enumerateKeysAndObjectsUsingBlock:^(NSIndexPath *indexPath, UICollectionViewLayoutAttributes *attributes, BOOL *innerStop) { 
         if (CGRectIntersectsRect(rect, attributes.frame)) { 
          [newArray2 addObject:attributes]; 
         } 
        }]; 
    
        attributesArrayInRect = [NSArray arrayWithArray:newArray2]; 
    
        return attributesArrayInRect; 
    } 
    

    :(same problem here

    4を参照してくださいブックシェルフバーが時々私はアイテム

    enter image description here

    の順序を変更するとき、私はここで重要なコードの一部をリストに移動しますあなたがこの投稿を読んで助言や提案をするのに十分なほどの患者であれば、私は感謝しています。 問題。前もって感謝します。

    +0

    Appleのフォーラムに投稿があります。 DecorationViewの重複はバグだと思われます。 https://devforums.apple.com/message/773776#773776 –

    +0

    実際には、prepareLayout関数の一部としてコレクションビューのコンテンツサイズを計算する必要があります。そのままにしておくと、コレクションビューに表示されているアイテムの数に関係なく、100ピクセル下までしかスクロールできません。 – Ash

    答えて

    0

    最後の行を確認し、最初の行と同じ[self.collectionView ScrollEnable:NO]を実行して、collectionViewとcollectionViewCellの背景色を設定し、そのブックシェルフイメージをバックグラウンドビューで設定することをお勧めします。

    関連する問題