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
////YYVIEWCONTROLLER.M//Nine Gongge exercises////Created by Hole Medical has on 14-5-22.//Copyright (c) 2014 itcast. All rights reserved.//#import "YYViewController.h"@interfaceYyviewcontroller () @property (nonatomic,strong) Nsarray*apps;@end@implementationYyviewcontroller//1. Loading Data-(Nsarray *) apps{if(!_apps) {NSString*path=[[nsbundle Mainbundle]pathforresource:@"app.plist"Oftype:nil]; _apps=[Nsarray Arraywithcontentsoffile:path]; } return_apps;}- (void) viewdidload{[Super Viewdidload]; NSLog (@"%d", Self.apps.count); //2. Complete the layout design//three columns intTotalloc=3; CGFloat APPVIEWW= the; CGFloat APPVIEWH= -; CGFloat margin= (SELF.VIEW.FRAME.SIZE.WIDTH-TOTALLOC*APPVIEWW)/(totalloc+1); intCount=Self.apps.count; for(intI=0; i<count; i++) { intRow=i/totalloc;//Line number//1/3=0,2/3=0,3/3=1; intLoc=i%totalloc;//column numbercgfloat appviewx=margin+ (MARGIN+APPVIEWW) *Loc; CGFloat Appviewy=margin+ (MARGIN+APPVIEWH) *Row; //Creating UIView ControlsUIView *appview=[[UIView Alloc]initwithframe:cgrectmake (Appviewx, Appviewy, APPVIEWW, APPVIEWH)]; //[Appview setbackgroundcolor:[uicolor purplecolor];[Self.view Addsubview:appview]; //to create a child view in a UIView controlUiimageview *appimageview=[[uiimageview Alloc]initwithframe:cgrectmake (0,0, the, -)]; UIImage*appimage=[uiimage imagenamed:self.apps[i][@"icon"]]; Appimageview.image=Appimage; [Appimageview Setcontentmode:uiviewcontentmodescaleaspectfit]; //NSLog (@ "%@", self.apps[i][@ "icon"]);[Appview Addsubview:appimageview]; //Create a text labelUILabel *applable=[[uilabel Alloc]initwithframe:cgrectmake (0, -, the, -)]; [Applable settext:self.apps[i][@"name"]]; [Applable Settextalignment:nstextalignmentcenter]; [Applable setfont:[uifont systemfontofsize:12.0]]; [Appview addsubview:applable]; //Create buttonUIButton *appbtn=[UIButton Buttonwithtype:uibuttontypecustom]; Appbtn.frame= CGRectMake (Ten, -, -, -); [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{//Animated LabelsUILabel *animalab=[[uilabel Alloc]initwithframe:cgrectmake (self.view.center.x- -, self.view.center.y+ -, $, +)]; [Animalab SetText:@"Download Successful"]; Animalab.font=[uifont systemfontofsize:12.0]; [Animalab Setbackgroundcolor:[uicolor Browncolor]; [Animalab Setalpha:0]; [Self.view Addsubview:animalab]; //[UIView Beginanimations:nil context:nil];//[Animalab setalpha:1];//[UIView setanimationduration:4.0];//[UIView commitanimations]; //after the execution, we have to delete this, we recommend using block animation[UIView animatewithduration:4.0animations:^{[Animalab setalpha:1]; } Completion:^(BOOL finished) {//[Self.view re]; }];}- (void) didreceivememorywarning{[Super didreceivememorywarning];}@end
Execution effect:
iOS development UI chapter--Nine grid coordinate calculation