Nine Gongge algorithm
The implementation of the nine Gongge in iOS development is not much, and there are better implementations behind it, but what you need to know as a programmer is the implementation of the nine lattice algorithm.
One: The realization of ideas:
- (1) Make clear what each piece is used for. View
- (2) Identify the parent-child relationship between each view, with only one parent view and a large number of child views.
- (3) You can first try to add a lattice, and finally consider using a for loop, complete the creation of all UIView
- (4) Load the app data, create the corresponding number of squares according to the data length
- (5) Adding child controls inside the lattice
- (6) Assembling data for internal child controls
Second: the implementation of the algorithm
1 /*2 * Total number of columns3 */4Nsuinteger Totalloc =3;5 6 /*7 * View width and height8 */9CGFloat SHOPW = the;TenCGFloat Shoph = -; One A /* - * The interval between each view - */ theCGFloat margin = (Self.view.frame.size.width-totalloc * shopw)/(Totalloc +1); - - /* - * Total number of view + */ -Nsuinteger count = A; + A /* at * The total number of columns is used to get the corresponding rows and columns in addition to the remainder . - */ -Nsuinteger loc = count/Totalloc; -Nsuinteger row = count%Totalloc; - - /* in * x and y for view - */ toCGFloat shopx = margin + (margin + shopw) *Row; +CGFloat shopy = margin + (margin + shoph) *Loc; - the * /* $ * Create a custom view, set the background color, add to the interfacePanax Notoginseng */ -UIView *SHOPV =[[UIView alloc] Initwithframe:cgrectmake (shopx, Shopy, SHOPW, Shoph)]; theShopv.backgroundcolor =[Uicolor Lightgraycolor]; + [Self.shopview ADDSUBVIEW:SHOPV]; A the /* + * Create Uiimageview to place pictures, set frame and add to Custom view - */ $Uiimageview *imagev =[[Uiimageview alloc] init]; $Imagev.frame = CGRectMake (0,0, the, the); - [SHOPV Addsubview:imagev]; - the /* - * Create Uilabel to place display text, set frame and add to Custom viewWuyi */ theUILabel *l =[[UILabel alloc] init]; -L.frame = CGRectMake (0, the, the, -); WuL.textalignment =Nstextalignmentcenter; -[SHOPV addsubview:l];
Third: The final realization of the effect is almost like this
Note: In the later study we will learn to use Uicollectionview and iOS9 new features Uistackview can be very simple implementation of nine Gongge, the following article inside we see!
iOS Development--ui & nine Gongge algorithm