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
//
Txviewcontroller.m
Cock Silk counter-day-Application Management 01
//
Created by Xin on 14-10-4.
Copyright (c) 2014 Liang. All rights reserved.
//
#import "TXViewController.h"
@interface Txviewcontroller ()
/**
* Send and drop application information
*/
@property (nonatomic, strong) Nsarray *apps;
@end
@implementation Txviewcontroller
-(void) viewdidload
{
[Super Viewdidload];
Add App Info
Total number of columns (up to 3 columns in one row)
int totalcolumns = 3;
Application Dimensions
CGFloat APPW = 85;
CGFloat APPH = 90;
Clearance = (width of controller view -3* application width)/4
CGFloat Marginx = (Self.view.frame.size.width-totalcolumns * appw)/(Totalcolumns + 1);
CGFloat marginy = 15;
Create a box based on the number of applications
for (int index = 0; index < self.apps.count; index++) {
3.1 Create a small box
UIView *appview = [[UIView alloc]init];
Calculate the position of the box
Calculate row and column numbers
int row = Index/totalcolumns;
int col = index% Totalcolumns;
Calculate x and Y
CGFloat AppX = Marginx + (APPW + Marginx) *col;
CGFloat appy = + (APPW + marginy) * ROW;
Set frame
Appview.frame = CGRectMake (AppX, Appy, APPW, APPH);
Add a box to the view of the controller
[Self.view Addsubview:appview];
Create an inner Gizmo
Application Information for index
Nsdictionary *appinfo = Self.apps[index];
Add a picture
Uiimageview *iconview = [[Uiimageview alloc] init];
Set Location
CGFloat iconw = 45;
CGFloat iconh = 45;
CGFloat IconX = (appw-iconw) *0.5;
CGFloat icony = 0;
Iconview.frame = CGRectMake (IconX, Icony, Iconw, Iconh);
Set up a picture
Iconview.image = [UIImage imagenamed:appinfo[@ "icon"];
[Appview Addsubview:iconview];
Add a Name
UILabel * namelable = [[UILabel alloc] init];
Set Location
CGFloat Namew = APPW;
CGFloat Nameh = 20;
CGFloat NameX = 0;
CGFloat Namey = Icony + iconh;
Namelable.frame = CGRectMake (NameX, Namey, Namew, Nameh);
Set text
Namelable.text = appinfo[@ "name"];
Set font
Namelable.font = [Uifont systemfontofsize:12];
Set up in a text play
Namelable.textalignment = Nstextalignmentcenter;
[Appview addsubview:namelable];
Add Download button
UIButton * downloabtn = [[UIButton alloc] init];
Set Location
CGFloat DOWNLOADX = 12;
CGFloat downloady = Namey +nameh;
CGFloat DOWNLOADW = appW-2 * DOWNLOADX;
CGFloat downloadh = 20;
Downloabtn.frame = CGRectMake (DOWNLOADX, Downloady, DOWNLOADW, downloadh);
Set the default background
[Downloabtn setbackgroundimage:[uiimage imagenamed:@ "Buttongreen"] forstate:uicontrolstatenormal];
[Downloabtn setbackgroundimage:[uiimage imagenamed:@ "buttongreen_highlighted"] forstate:uicontrolstatehighlighted ];
[downloabtn settitle:@ "Download" forstate:uicontrolstatenormal];
[Appview ADDSUBVIEW:DOWNLOABTN];
}
}
-(Nsarray *) apps
{
if (_apps ==nil) {
Initialization
1. Get the full path of the plist
NSString *path = [[NSBundle mainbundle] pathforresource:@ "app.plist" oftype:nil];
2. Loading an array
_apps = [Nsarray Arraywithcontentsoffile:path];
}
return _apps;
}
Execution effect:
Application management of iOS Development UI Chapter nine Gongge coordinate calculation