iOS Development--ui & nine Gongge algorithm

Source: Internet
Author: User

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

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.