iOS development UI chapter-nine grid coordinate calculation

Source: Internet
Author: User

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 //SLVIEWCONTROLLER.M3 //Nine Gongge exercises4 //5 //Created by Apple on 14-5-21.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "SLViewController.h"Ten  One @interfaceSlviewcontroller () A@property (nonatomic,strong) Nsarray *apps; - @end -  the @implementationSlviewcontroller -  -  - //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 (@"%ld", self.apps.count); +      -     //2. Complete the layout design the     //three columns *     intTotalloc =3; $CGFloat APPVIEWW = the;Panax NotoginsengCGFloat APPVIEWH = -; -      theCGFloat margin = (Self.view.frame.size.width-totalloc * appvieww)/(Totalloc +1); +     intCount =Self.apps.count; A      for(inti =0; I < count; i++) { the         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         //Creating UIView Controls -UIView *appview =[[UIView Alloc]initwithframe:cgrectmake (Appviewx, Appviewy, APPVIEWW, APPVIEWH)];Wuyi         //[Appview setbackgroundcolor:[uicolor purplecolor]; the [Self.view Addsubview:appview]; -          Wu          -         //to create a child view in a UIView control AboutUiimageview *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"]); A [Appview Addsubview:appimageview]; +          the         //Create a text label -UILabel *applable = [[UILabel alloc]initwithframe:cgrectmake (0, -, the, -)]; $[Applable settext:self.apps[i][@"name"]]; the [applable Settextalignment:nstextalignmentcenter]; the[Applable Setfont:[uifont Systemfontofsize:12.0]]; the [Appview addsubview:applable]; the          -         //Create button inUIButton *appbtn =[UIButton Buttonwithtype:uibuttontypecustom]; theAppbtn.frame = CGRectMake (Ten, -, -, -); the[Appbtn setbackgroundimage:[uiimage imagenamed:@"Buttongreen"] forstate:uicontrolstatenormal]; About[Appbtn setbackgroundimage:[uiimage imagenamed:@"buttongreen_highlighted"] forstate:uicontrolstatehighlighted]; the[Appbtn Settitle:@"Download"Forstate:uicontrolstatenormal]; theAppbtn.titleLabel.font = [Uifont systemfontofsize:12.0]; the [Appview addsubview:appbtn]; +          - [appbtn addtarget:self Action: @selector (click) forcontrolevents:uicontroleventtouchupinside]; the     }Bayi      the } the  --(void) Click - { the     //Animated Labels theUILabel *animalab = [[UILabel alloc]initwithframe:cgrectmake (self.view.center.x- -, self.view.center.y+ -, $, +)]; the[Animalab SetText:@"Download Successful"]; theAnimalab.font = [Uifont systemfontofsize:12.0]; - [Animalab setbackgroundcolor:[uicolor browncolor]; the[Animalab Setalpha:0]; the [Self.view Addsubview:animalab]; the     94     /* the [UIView Beginanimations:nil context:nil]; the [Animalab setalpha:1]; the [UIView setanimationduration:4.0];98 [UIView commitanimations]; About     */ -     101     //after the execution, we have to delete this, we recommend using block animation102[UIView animatewithduration:4.0animations:^{103[Animalab Setalpha:1];104} completion:^(BOOL finished) { the //[Self.view Removefromsuperview];106     }];107 }108 109 @end

Execution effect:

iOS development UI chapter-nine grid coordinate calculation

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.