First, the requirements
Complete the following layout
Second, analysis
Look for the law on the left, each uiview x-coordinate and y-coordinate.
Third, 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
Iv. code Examples
1//2//YYVIEWCONTROLLER.M 3//Nine Practice 4//5//Created by Hole doctor on 14-5-22. 6//Copyright (c) 2014 itcast. All rights reserved. 7//8 9 #import "YYViewController.h" @interface Yyviewcontroller () @property (nonatomic,strong) Nsarray *apps ; @end @implementation Yyviewcontroller 16 17 18//1. Load Data-(Nsarray *) Apps (!_apps) {22 NSString *path=[[nsbundle mainbundle]pathforresource:@ "App.plist" oftype:nil]; _apps=[nsarray Arraywithcontentsoffile:path]; _apps return; -(void) viewdidload {[Super Viewdidload]; NSLog (@ "%d", Self.apps.count); 32 33//2. Finish into a layout design 34 35//three columns totalloc=3 int; Panax Notoginseng cgfloat appvieww=80; CGFloat appviewh=90; CGFloat margin= (SELF.VIEW.FRAME.SIZE.WIDTH-TOTALLOC*APPVIEWW)/(totalloc+1); Count=self.apps.count int; i<count; i++ {i=0 int row=i/totalloc;//line number 44//1/3=0,2/3=0,3/3=1; loc=i%totalloc;//int, cgfloat appviewx=margin+ (MARGIN+APPVIEWW) *loc; CGFloat appviewy=margin+ (MARGIN+APPVIEWH) *row; 49 50 51//Create UIView control UIView *appview=[[uiview Alloc]initwithframe:cgrectmake (appview X, Appviewy, APPVIEWW, APPVIEWH)]; //[appview Setbackgroundcolor:[uicolor Purplecolor]]; [Self.view Addsubview:appview]; 55 56 57//Create a child view in a UIView control Uiimageview *appimageview=[[uiimageview Alloc]initwithfram E:cgrectmake (0, 0, 80, 50)]; UIImage *appimage=[uiimage imagenamed:self.apps[i][@ "icon"]; Appimageview.image=appimage; [Appimageview Setcontentmode:uiviewcontentmodescaleaspectfit]; +//NSLog (@ "%@", self.apps[i][@ "icon"]); [Appview Addsubview:appimageview]; 64 65//Create text label UILabel *applable=[[uilabel alloc]initwithframe:cgrectmake (0, 50, 80, 20)]; 67 [applable settext:self.apps[i][@ "name"]; [Applable Settextalignment:nstextalignmentcenter]; [Applable Setfont:[uifont systemfontofsize:12.0]]; [Appview addsubview:applable]; 71 72//Create button UIButton *appbtn=[uibutton Buttonwithtype:uibuttontypecustom]; Appbtn.frame= CGRectMake (10, 70, 60, 20); [Appbtn setbackgroundimage:[uiimage imagenamed:@ "Buttongreen"] forstate:uicontrolstatenormal]; [Appbtn setbackgroundimage:[uiimage imagenamed:@ "buttongreen_highlighted"] forstate:uicontrolstatehighlighted]; [appbtn settitle:@ "Download" forstate:uicontrolstatenormal]; Appbtn.titlelabel.font=[uifont systemfontofsize:12.0]; [Appview ADDSUBVIEW:APPBTN]; [Appbtn addtarget:self Action: @selector (click) forcontrolevents:uicontroleventtouchupinside]; (void) Click 87 {88//animated label UILabel *animalab=[[uilabel ALLOC]INITWITHFRAme:cgrectmake (self.view.center.x-100, SELF.VIEW.CENTER.Y+20, 200, 40)]; [Animalab settext:@ "Download Success"]; Animalab.font=[uifont systemfontofsize:12.0]; [Animalab Setbackgroundcolor:[uicolor Browncolor]]; [Animalab setalpha:0]; 94 [Self.view Addsubview:animalab]; [UIView Beginanimations:nil Context:nil]; //[Animalab setalpha:1]; 98//[UIView setanimationduration:4.0]; //[UIView commitanimations];100 101//After execution, we have to delete this, we recommend using block Animation 102 103 [UIView animatewithduration : 4.0 animations:^{104 [Animalab setalpha:1];105} completion:^ (BOOL finished) {106//[self.view re];107 }];108}109-(void) didReceiveMemoryWarning111 {didreceivememorywarning];113}114 @end
Execution effect:
iOS development UI chapter-nine grid coordinate calculation