iOS implements Bookshelf layout style "Some ebook's homepage"

Source: Internet
Author: User
Tags uikit

This article realizes the similar ebook homepage, uses to display the book or the novel layout page, the Bookshelf list "the IPhone6 simulator", the screen size has not yet adapted, just makes a simple demo "The Pure Code Realization Way"

Implementations use Uicollectionview and Uicollectionviewflowlayout. For a detailed explanation of Uicollectionview, please refer to http://blog.csdn.net/meegomeego/article/details/16953489

First, the realization of layout of the Decorationview

// //   FWBookShelfDecarationViewCollectionReusableView.h//  fwpersonalapp//  //  Created by hzkmn on 16/2/18. //   copyright©2016 year Forrstwoo. All rights reserved. //  <UIKit/UIKit.h>externconst  kdecorationviewheight; @interface Fwbookshelfdecarationview:uicollectionreusableview@end
FWBookShelfDecarationView.h

////fwbookshelfdecarationviewcollectionreusableview.m//Fwpersonalapp////Created by Hzkmn on 16/2/18.//copyright©2016 year Forrstwoo. All rights reserved.//#import"FWBookShelfDecarationView.h"NsintegerConstKdecorationviewheight =216; @implementation Fwbookshelfdecarationview-(Instancetype) initWithFrame: (cgrect) frame{if(self =[Super Initwithframe:frame]) {Uiimageview*img = [[Uiimageview alloc] Initwithframe:cgrectmake (0,0, Screensize.width, Kdecorationviewheight)]; Img.image= [UIImage imagenamed:@"Boolshelf.png"];    [Self addsubview:img]; }        returnSelf ;} @end
fwbookshelfdecarationview.m

The Fwbookshelfdecarationview class is very simple just to define the background image of Decarationview, on the graph.

Ii. downloading and importing re-ordered third-party layouts for re-layout after we move books

See http://www.cnblogs.com/salam/p/5205919.html

Third, to achieve their own layout

First, inherit the Lxreorderablecollectionviewflowlayout, and let the class have the reordering function.

////FWBookshelfCollectionViewLayout.h//Fwpersonalapp////Created by Hzkmn on 16/2/18.//copyright©2016 year Forrstwoo. All rights reserved.//#import"LXReorderableCollectionViewFlowLayout.h"externNSString *Constfwbookshelfcollectionviewlayoutdecorationviewkind; @interface fwbookshelfcollectionviewlayout: Lxreorderablecollectionviewflowlayout@end
FWBookshelfCollectionViewLayout.h

////FWBOOKSHELFCOLLECTIONVIEWLAYOUT.M//Fwpersonalapp////Created by Hzkmn on 16/2/18.//copyright©2016 year Forrstwoo. All rights reserved.//#import"FWBookshelfCollectionViewLayout.h"#import"FWBookShelfDecarationView.h"NSString*ConstFwbookshelfcollectionviewlayoutdecorationviewkind =@"Fwbookshelfcollectionviewlayoutdecorationviewkind"; @interface fwbookshelfcollectionviewlayout () @property (nonatomic, strong) Nsdictionary*bookshelfrectanges, @property nsinteger countofrow; @end @implementation fwbookshelfcollectionviewlayout- (void) preparelayout{[Super Preparelayout]; [Self Registerclass:[fwbookshelfdecarationviewclass] Fordecorationviewofkind:fwbookshelfcollectionviewlayoutdecorationviewkind]; Nsmutabledictionary*dictionary =[Nsmutabledictionary dictionary]; Nsinteger ItemCount= [Self.collectionview numberofitemsinsection:0]; Self.countofrow= Ceilf (ItemCount/3.0);  for(introw =0; Row < Self.countofrow; row++) {Dictionary[[nsindexpath indexpathforitem:row insection:0]] = [Nsvalue valuewithcgrect:cgrectmake (0, Kdecorationviewheight *row, Screensize.width, kdecorationviewheight)]; } self.bookshelfrectanges=[Nsdictionary dictionarywithdictionary:dictionary];}#pragmaMark Runtime Layout calculations-(Nsarray *) Layoutattributesforelementsinrect: (cgrect) rect{//Call Super so flow layout can return default attributes for all cells, headers, and footers//Note:flow layout have already taken care of the Cell view layout attributes!:)Nsarray *array =[Super Layoutattributesforelementsinrect:rect]; //Create a mutable copy so we can add layout attributes for any shelfs that//The frames that intersect the rect , the CollectionView is interested inNsmutablearray *newarray =[Array mutablecopy]; //NSLog (@ "in rect:%@", Nsstringfromcgrect (rect)); //Add any decoration views (shelves) who's rect intersects with the//CGRect passed to the layout by the CollectionView[Self.bookshelfrectanges enumeratekeysandobjectsusingblock:^ (ID key, id shelfrect, BOOL *stop) {        //NSLog (@ "[Shelfrect cgrectvalue]:%@", Nsstringfromcgrect ([Shelfrect cgrectvalue]);                if(Cgrectintersectsrect ([Shelfrect cgrectvalue], rect)) {uicollectionviewlayoutattributes*shelfattributes =[Self Layoutattributesfordecorationviewofkind:fwbookshelfcollectionviewlayoutdecorationviewkind            Atindexpath:key];        [NewArray addobject:shelfattributes];        }    }];  for(inti =0; i < [Self.collectionview numberofitemsinsection:0]; i++) {Nsindexpath*indexpath = [Nsindexpath indexpathforitem:i insection:0];    [NewArray addobject:[self Layoutattributesforitematindexpath:indexpath]; }        return[NewArray copy];}-(Uicollectionviewlayoutattributes *) Layoutattributesforitematindexpath: (Nsindexpath *) indexpath{//NSLog (@ "%@", nsstringfromcgsize ([self screensize]); 375 667Uicollectionviewlayoutattributes *attris =[Uicollectionviewlayoutattributes Layoutattributesforcellwithindexpath:indexpath]; Nsinteger CurrentRow= Indexpath.item/3; CGRect Frame= CGRectMake ( -+ (Indexpath.item%3) * (Kcellwidth +17.5), *+ CurrentRow * (Kcellheight + $), Kcellwidth, kcellheight); Attris.frame=frame; Attris.zindex=1; returnAttris;}-(Uicollectionviewlayoutattributes *) Layoutattributesfordecorationviewofkind: (NSString *) decorationViewKind Atindexpath: (Nsindexpath *) indexpath{ID shelfrect=Self.bookshelfrectanges[indexpath]; //This should never happen, but just in case ...    if(!shelfrect)returnNil; Uicollectionviewlayoutattributes*attributes =[Uicollectionviewlayoutattributes Layoutattributesfordecorationviewofkind:decorationviewkind    Withindexpath:indexpath]; Attributes.frame=[Shelfrect Cgrectvalue]; //NSLog (@ "uicollectionviewlayoutattributes:.%@", Nsstringfromcgrect ([Shelfrect cgrectvalue]);Attributes.zindex= -1;//shelves go behind other views        returnattributes;}-(cgsize) collectionviewcontentsize{cgsize contentsize= Cgsizemake (Self.collectionView.bounds.size.width, Self.countofrow * kdecorationviewheight + -); returnContentsize;} @end
FWBOOKSHELFCOLLECTIONVIEWLAYOUT.M

Iv. Application

////FWANCIENTPOETRYCOLLECTIONVIEWCONTROLLER.M//Fwpersonalapp////Created by Hzkmn on 16/2/17.//copyright©2016 year Forrstwoo. All rights reserved.//#import"FWAncientPoetryCollectionViewController.h"#import"FWBookShelfDecarationView.h"#import"FWBookshelfCollectionViewLayout.h"#import"FWBookCategoryViewController.h"@interface Fwancientpoetrycollectionviewcontroller ()<lxreorderablecollectionviewdatasource, lxreorderablecollectionviewdelegateflowlayout>@property (nonatomic, strong) Nsmutablearray*Books, @end @implementation FwancientpoetrycollectionviewcontrollerStaticNSString *ConstCellreuseidentifier =@"Cell";- (void) viewdidload {[Super viewdidload]; Self.title=@"Ancient Books"; Self.collectionView.backgroundColor= [Uicolor colorwithpatternimage:[uiimage imagenamed:@"Bookshelfbackground.png"]]; [Self.collectionview Registerclass:[uicollectionviewcellclass] Forcellwithreuseidentifier:cellreuseidentifier]; Self.books=[[Nsmutablearray alloc] initwitharray:[self booknameofallbooks];}-(Nsarray *) booknameofallbooks{return[[Fwdatamanager getdataforpoetry] allKeys];}#pragmaMark <uicollectionviewdatasource>-(Nsinteger) Numberofsectionsincollectionview: (UICollectionView *) collectionview{return 1;}-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) section{return[Self.books Count];}-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) indexpath{Uicollectionviewcell*cell =[CollectionView dequeuereusablecellwithreuseidentifier:cellreuseidentifier Forindexpath:indexpath]; UIImage*image =[UIImage Imagenamed:self.books[indexpath.item]]; Cell.backgroundcolor=[Uicolor Colorwithpatternimage:image]; returncell;}- (void) CollectionView: (Uicollectionview *) CollectionView Itematindexpath: (Nsindexpath *) Fromindexpath Willmovetoindexpath: (Nsindexpath *) toindexpath{NSString*thebookname =Self.books[fromindexpath.item];    [Self.books RemoveObjectAtIndex:fromIndexPath.item]; [Self.books insertobject:thebookname AtIndex:toIndexPath.item];}#pragmamark-uicollectionviewdelegateflowlayout-(cgsize) CollectionView: (Uicollectionview *) CollectionView layout: ( Uicollectionviewlayout *) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) indexpath{returnCgsizemake (Kcellwidth, kcellheight);}- (void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) indexpath{Fwbookcategoryviewcontroller*VC =[[Fwbookcategoryviewcontroller alloc] Initwithurlstring:[[fwdatamanager getdataforpoetry] ObjectForKey:    Self.books[indexpath.item]]; [Self.navigationcontroller PUSHVIEWCONTROLLER:VC animated:yes];}- (void) didreceivememorywarning {[Super didreceivememorywarning];    [Self.books removeallobjects]; Self.books=Nil;} @end
FWANCIENTPOETRYCOLLECTIONVIEWCONTROLLER.M

iOS implements Bookshelf layout style "Some ebook's homepage"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.