Implement nine Gongge layouts in pure code mode

Source: Internet
Author: User

Functional Analysis:

1> Display application information in nine Gongge form

2> Click the Download button and then make the appropriate action

Analysis:

1> Find out the law of nine layout, set X and Y for each control

-Y value: determined by line number; Rounding operations (the number of columns for the control index/nine grid layout)

-X Value: determined by column number; Modulo operations (control index% of Gongge layout columns)

2> Each control contains several child controls, consider wrapping with view for easy calculation

3> extract important data from the nine layout for easy layout modification

Code implementation:

1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4 5 /** For storing app information*/6@property (nonatomic, strong) Nsarray *apps;7 8 @end9 Ten @implementationViewcontroller One  A //Lazy Loading --(Nsarray *) Apps - { the     if(!_apps) { -         //getting data from the Plist file -NSString *path = [[NSBundle mainbundle] Pathforresource:@"app.plist"Oftype:nil]; -_apps =[Nsarray Arraywithcontentsoffile:path]; +     } -     return_apps; + } A  at- (void) Viewdidload { - [Super Viewdidload]; -      -   -     //1. Number of columns for layout nine Gongge -Nsinteger Colsnum =3; in      -     //2. Size of each app toCGFloat APPW = the; +CGFloat APPH = -; -      the     //3. Set the spacing between each column = (width of Controller view-Number of columns * app width)/number of gaps *CGFloat Colsmargin = (Self.view.frame.size.width-colsnum * appw)/(Colsnum +1); $     //set the spacing between each linePanax NotoginsengCGFloat Rowsmargin = -; -      the     //4. Create according to the number of applications +      for(intindex =0; Index < Self.apps.count; index++) { A         //4.1 Creating an app theUIView *appview =[[UIView alloc] init]; +  -         //4.2 Calculating the location of the app $         //4.2.1 Calculating line numbers and column numbers $         introw = index/Colsnum; -         intCol = index%Colsnum; -          the         //4.2.2 Calculate x and Y (Note: Do not block by the height of the status bar on the top of the phone (20) ) -CGFloat AppX = colsmargin + col * (APPW +colsmargin);WuyiCGFloat appy = rowsmargin + row * (APPH +rowsmargin); the          -         //4.2.3 Set the app's frame WuAppview.frame =CGRectMake (AppX, Appy, APPW, APPH); -          About         //4.3 Adding a view to the controller $ [Self.view Addsubview:appview]; -          -         //4.4 Setting up the app's internal controls -         //4.4.1 Application information for index position ANsdictionary *appinfo =Self.apps[index]; +          the         //4.4.2 Add icon -Uiimageview *iconview =[[Uiimageview alloc] init]; $          the         //4.4.3 setting icon position theCGFloat iconw = $; theCGFloat Iconh = $; theCGFloat IconX = (appw-iconw) *0.5; -CGFloat icony =0; inIconview.frame =CGRectMake (IconX, Icony, Iconw, iconh); the          the         //4.4.4 setting up pictures AboutIconview.image = [UIImage imagenamed:appinfo[@"icon"]]; the [Appview Addsubview:iconview]; the          the         //4.4.5 Adding text +UILabel *namelabel =[[UILabel alloc] init]; -          the         //4.4.6 Setting Text positionBayiCGFloat Namew =APPW; theCGFloat Nameh = -; theCGFloat NameX =0; -CGFloat Namey = Icony +Iconh; -Namelabel.frame =CGRectMake (NameX, Namey, Namew, Nameh); the          the         //4.4.7 Setting Text theNamelabel.text = appinfo[@"name"]; theNamelabel.font = [Uifont systemfontofsize: -]; -Namelabel.textalignment =Nstextalignmentcenter; the [Appview Addsubview:namelabel]; the          the         //4.4.8 Add button94UIButton *downloadbtn =[[UIButton alloc] init]; the          the         //4.4.9 Setting the button position theCGFloat DOWNLOADX = A;98CGFloat Downloady = Namey +Nameh; AboutCGFloat DOWNLOADW = APPW-2*DOWNLOADX; -CGFloat downloadh = -;101Downloadbtn.frame =CGRectMake (DOWNLOADX, Downloady, DOWNLOADW, downloadh);102         103         //4.4.10 Settings button104[Downloadbtn setbackgroundimage:[uiimage imagenamed:@"Buttongreen"] forstate:uicontrolstatenormal]; the[Downloadbtn setbackgroundimage:[uiimage imagenamed:@"buttongreen_highlighted"] forstate:uicontrolstatehighlighted];106[Downloadbtn Settitle:@"Download"Forstate:uicontrolstatenormal];107DownloadBtn.titleLabel.font = [Uifont systemfontofsize: -];108 [downloadbtn addtarget:self Action: @selector (Downloadclick) forcontrolevents:uicontroleventtouchupinside];109 [Appview addsubview:downloadbtn]; the     }111      the }113  the- (void) Downloadclick the { theNSLog (@"%s", __func__);117 }118 @end

Implement nine Gongge layouts in pure code mode

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.