ios6 - Create a book shelf by using UICollectionView -
i'm going create book shelf in 1 of projects. basic requirement follows:
- similar ibooks bookshelf
- supports both orientations well
- supports kinds of ios devices (different resolutions)
- supports deleting , inserting items
- supports reordering of items long press gesture
- shows hidden logo when first row pulled down
the uicollectionview first option occurred me. supports grid cells. googled , found useful tutorials:
bryan hansen's uicollectionview custom layout tutorial
mark pospesel's how add decoration view uicollectionview
lxreorderablecollectionviewflowlayout
and here result: (please ignore color inconsistency problem because of graphics chose not perfect yet.)

what did:
- created custom layout creating class inherited lxreorderablecollectionviewflowlayout (for reordering purposes) inherited uicollectionflowlayout
- added decoration view showing logo
- added decoration view showing bookshelfs
but ran few problems:
1. can't scroll @ if items can shown in 1 screen
then added following code, make contentsize bigger
- (cgsize)collectionviewcontentsize { return cgsizemake(self.collectionview.bounds.size.width, self.collectionview.bounds.size.height+100); } then can scroll now. here pull down first row:

you can see the decoration view logo working.
2. got second set of problems when pull last row:

you can see decoration view not added @ green box part.
3. background of decoration view bookshelf getting darker , darker. (please refer same problem here
4. book shelf bar moves when reorder items

i list of important code here:
- (void)preparelayout { // call super flow layout can math cells, headers, , 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 shelves go in 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)); (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 doesn't have data (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 (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; } i'll appreciate if you're patient enough read post , provide advice or suggestions. , i'll post complete source code if can fix issues. thank in advance.
my advice , suggestion need check last row , [self.collectionview scrollenable:no] same first row, set background color clear of collectionview , collectionviewcell , bookshelf image set on background view.
Comments
Post a Comment