標籤:
一、要求
完成下面的布局
二、分析
尋找左邊的規律,每一個uiview的x座標和y座標。
三、實現思路
(1)明確每一塊用得是什麼view
(2)明確每個view之間的父子關係,每個視圖都只有一個父視圖,擁有很多的子視圖。
(3)可以先嘗試逐個的添加格子,最後考慮使用for迴圈,完成所有uiview的建立
(4)載入app資料,根據資料長度建立對應個數的格子
(5)添加格子內部的子控制項
(6)給內部的子控制項裝配資料
四、程式碼範例
//// YYViewController.m// 九宮格練習//// Created by 孔醫己 on 14-5-22.// Copyright (c) 2014年 itcast. All rights reserved.//#import "YYViewController.h"@interface YYViewController ()@property(nonatomic,strong)NSArray *apps;@end@implementation YYViewController//1.載入資料- (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.完成布局設計 //三列 int totalloc=3; CGFloat appvieww=80; CGFloat appviewh=90; CGFloat margin=(self.view.frame.size.width-totalloc*appvieww)/(totalloc+1); int count=self.apps.count; for (int i=0; i<count; i++) { int row=i/totalloc;//行號 //1/3=0,2/3=0,3/3=1; int loc=i%totalloc;//列號 CGFloat appviewx=margin+(margin+appvieww)*loc; CGFloat appviewy=margin+(margin+appviewh)*row; //建立uiview控制項 UIView *appview=[[UIView alloc]initWithFrame:CGRectMake(appviewx, appviewy, appvieww, appviewh)]; //[appview setBackgroundColor:[UIColor purpleColor]]; [self.view addSubview:appview]; //建立uiview控制項中的子視圖 UIImageView *appimageview=[[UIImageView alloc]initWithFrame: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]; //建立文字標籤 UILabel *applable=[[UILabel alloc]initWithFrame:CGRectMake(0, 50, 80, 20)]; [applable setText:self.apps[i][@"name"]]; [applable setTextAlignment:NSTextAlignmentCenter]; [applable setFont:[UIFont systemFontOfSize:12.0]]; [appview addSubview:applable]; //建立按鈕 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:@"下載" forState:UIControlStateNormal]; appbtn.titleLabel.font=[UIFont systemFontOfSize:12.0]; [appview addSubview:appbtn]; [appbtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside]; }}-(void)click{ //動畫標籤 UILabel *animalab=[[UILabel alloc]initWithFrame:CGRectMake(self.view.center.x-100, self.view.center.y+20, 200, 40)]; [animalab setText:@"下載成功"]; 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]; //執行完之後,還得把這給刪除了,推薦使用block動畫 [UIView animateWithDuration:4.0 animations:^{ [animalab setAlpha:1]; } completion:^(BOOL finished) { //[self.view re]; }];}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning];}@end
執行效果:
iOS開發UI篇——九宮格座標計算