iOS development UI chapter-nine grid coordinate calculation
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 Gongge exercises 4 // 5 //Created by Hole Medical has on 14-5-22. 6 //Copyright (c) 2014 itcast. All rights reserved. 7 // 8 9 #import "YYViewController.h" Ten One @interfaceYyviewcontroller () A@property (nonatomic,strong) Nsarray *apps; - @end - the @implementationYyviewcontroller - - - //1. Loading Data +-(Nsarray *) Apps - { + if(!_apps) { ANSString *path=[[nsbundle Mainbundle]pathforresource:@"app.plist"Oftype:nil]; at_apps=[Nsarray Arraywithcontentsoffile:path]; - } - return_apps; - } - -- (void) Viewdidload in { -[Super Viewdidload]; toNSLog (@"%d", Self.apps.count); + - //2. Complete the layout design the * //three columns $ intTotalloc=3; Panax NotoginsengCGFloat appvieww= the; -CGFloat appviewh= -; the +CGFloat margin= (SELF.VIEW.FRAME.SIZE.WIDTH-TOTALLOC*APPVIEWW)/(totalloc+1); A intCount=Self.apps.count; the for(intI=0; i<count; i++) { + intRow=i/totalloc;//Line number - //1/3=0,2/3=0,3/3=1; $ intLoc=i%totalloc;//column number $ -CGFloat appviewx=margin+ (MARGIN+APPVIEWW) *Loc; -CGFloat appviewy=margin+ (MARGIN+APPVIEWH) *Row; the - Wuyi //Creating UIView Controls theUIView *appview=[[UIView Alloc]initwithframe:cgrectmake (Appviewx, Appviewy, APPVIEWW, APPVIEWH)]; - //[Appview setbackgroundcolor:[uicolor purplecolor]; Wu[Self.view Addsubview:appview]; - About $ //to create a child view in a UIView control -Uiimageview *appimageview=[[uiimageview Alloc]initwithframe:cgrectmake (0,0, the, -)]; -UIImage *appimage=[uiimage imagenamed:self.apps[i][@"icon"]]; -Appimageview.image=Appimage; A[Appimageview Setcontentmode:uiviewcontentmodescaleaspectfit]; + //NSLog (@ "%@", self.apps[i][@ "icon"]); the[Appview Addsubview:appimageview]; - $ //Create a text label theUILabel *applable=[[uilabel Alloc]initwithframe:cgrectmake (0, -, the, -)]; the[Applable settext:self.apps[i][@"name"]]; the[applable Settextalignment:nstextalignmentcenter]; the[Applable Setfont:[uifont Systemfontofsize:12.0]]; -[Appview addsubview:applable]; in the //Create button theUIButton *appbtn=[UIButton Buttonwithtype:uibuttontypecustom]; AboutAppbtn.frame= CGRectMake (Ten, -, -, -); the[Appbtn setbackgroundimage:[uiimage imagenamed:@"Buttongreen"] Forstate:uicontrolstatenormal]; the[Appbtn setbackgroundimage:[uiimage imagenamed:@"buttongreen_highlighted"] forstate:uicontrolstatehighlighted]; the[Appbtn Settitle:@"Download"Forstate:uicontrolstatenormal]; +Appbtn.titlelabel.font=[uifont systemfontofsize:12.0]; -[Appview addsubview:appbtn]; the Bayi[appbtn addtarget:self Action: @selector (click) forcontrolevents:uicontroleventtouchupinside]; the } the - } - the-(void) Click the { the //Animated Labels theUILabel *animalab=[[uilabel Alloc]initwithframe:cgrectmake (self.view.center.x- -, self.view.center.y+ -, $, +)]; -[Animalab SetText:@"Download Successful"]; theAnimalab.font=[uifont systemfontofsize:12.0]; the[Animalab setbackgroundcolor:[uicolor browncolor]; the[Animalab Setalpha:0]; 94[Self.view Addsubview:animalab]; the the //[UIView Beginanimations:nil context:nil]; the //[Animalab setalpha:1]; 98 //[UIView setanimationduration:4.0]; About //[UIView commitanimations]; - 101 //after the execution, we have to delete this, we recommend using block animation102 103[UIView animatewithduration:4.0animations:^{104[Animalab Setalpha:1]; the} completion:^(BOOL finished) {106 //[Self.view re];107 }];108 }109 the- (void) didreceivememorywarning111 { the[Super didreceivememorywarning];113 } the
Execution effect:
iOS development UI chapter-nine grid coordinate calculation