一、 九宫格布局方式;

 

A 明确布局的列数

B 确定宽高

C 计算间隔

D 计算x、y的值 (x 列索引 y 行索引)

E 完成布局

 

//1 set cloum

int cloum = 3;

 

//2 set view's width and height

CGFloat yellowviewWidth = 95;

CGFloat yellowviewHeight = 80;

 

// 3 set margin

CGFloat margin = (self.view.frame.size.width -cloum*yellowviewWidth)/(cloum+1);

 

// 4 init X and Y margin;

CGFloat Ymargin = margin;

CGFloat Xmargin = margin;

 

//5 set the yellowview

UIView *yellowview = [[UIView alloc] initWithFrame :CGRectMake(Xmargin,Ymargin,yellowviewWidth,yellowviewHeight)];

 

//6 根据循环 按顺序生成九宫格

int count = 14

for (int i=0;i<count ;i++)

{

//set index

NSInteger rowindex = count/colum;

NSInteger columindex = count % colum;

//set margin

Ymargin = (rowindex*yellowviewHeight) +(rowindex+1)*margin;

Xmargin = (columindex*yellowviewWidth) +(columindex+1)*margin;

//set view

UIView *yellowview = [[UIView alloc]initWithFrame:CGRectMake(Xmargin,Ymargin,yellowviewWidth,yellowviewHeight)];

}

 

二、根据plist中字典的数量进行生成一定规格的view

1、 获取plist中包含字典的数组

NSBundle *mainBundle =[NSBundel mainBundle] ;

NSString *path = [mainBundle pathForResource:@"app.plist" ofType:nil];

NSArray *arry = [NSArray arrayWithContentsOfFile :path];

//什么是懒加载? 为什么用NSArray而不是NSMutableArray  使用不可变数组,因为该数组为内部取出的 如果非必要 不介意直接修改数据 另外,相比不可变数组,可变数组对内存的消耗相对快

懒加载数据:

-(NSArray *) dataArray

{

if (nil==dataArray)

{

1)获取路径

NSString *path=[[NSBundel mainBundle ]pathForResource: @"app.plist" ofType:nil];

2)获取数组

dataArray=[NSArray arrayWithContentsOfFile:path];

}

}

3)取出数组中的字典

NSDictionary *dict =self.dataArray[0];

此处不把self.dataArray写成_dataArray 是因为懒加载的时候,数组必须要实例化,而dataArray对象相当于重写了dataArray的get方法,其实质就是调用dataArray的get方法

dict[@“name”]

 

3、当加载的数据不是强制限定而是由plist中的字典数量决定时,只需要将上述的count变量改

count = self.array.count;

 

三、将字典转化成模型

 

1、关系

了解对象的概念,字典,是一个对象,model也是对象,关联两者相对应的联系

NSDirctionary _对象

Key:name _“去哪”

Key: icon_“father.png”

 

AppModel :NSObject

@property (nonatomic,copy)NSString *name;

@property(nonatomi,copy)NSString * icon;

这些Key值就是用来描述对象(数据),通过创建对象来描述数据,例如创建一个class来描述,设置对象的属性,与它的Key值对应

由上可看出 AppModel和NADirctionary都是对象的对应,而Appmodel中的属性则与字典中的Key值对应

 

2、对应方法

1)creat Appmodel.h/Appmodel.m

@interface Appmodel:NSObject

@property(nonatomic,copy)NSString *name;

@property(nonatomic,copy)NSString *icon;

@end

 

2)set the NSDictionary data to Appmodel :

for (int i=0;i<count ;i++)

{

//1 init the Appmodel

Appmodel *appmodel =[[Appmodel  alloc ]init];

//set the data

dict = arry[i];

appmodel.name= dict[@"name"];

appmodel.icon=dict[@"icon"];

}

 

这种方法的缺陷:可扩展性差,代码量大,假设字典中变量很多,或者调用Appmodel 的类文件很多 可以在Appmodel直接进行与字典间数据的传递用来监测参数类型的,现在id也可以

id或instancetype都可以作为参数返回值,此前,instancetype是可以

-(id) initWithDict:(NSDirctionary*)dict

{

//1 init superclass

if(self =[super init])

{

self.icon = dict[@"icon"];

self.name=dict[@"name"];

}

return self;

}

然后在调用Appmodel的类中,直接调用该方法就好

Appmodel *appmodel =[[Appmodel alloc ]initWithDict:dict];

namelabel.text = appmodel.name;

iconimage.image[UIImage imageNamed:appmodel.icon];

 

四、关于xib

1、button状态:Normal 默认,highLighted 高亮,selected:被选中;enabled:被禁用

2、xib与storyboard的区别:

xib与storyboard都是用来设计用户界面,xib是局部界面,轻量级,storyboard是整体界面,重量级,同时storyboard支持界面跳转

 

3、为什么查找xib的时候调用LoadNibName 因为xib文件在bundle中被打包之后,就是以nib结尾的文件

 

1)加载xib文件

UIView *yellowview = [[NSBundel mainBundel] LoadNibName :@"yellowview" owner:nil options:nil]];

返回的是一个数组 数组的排列是以xib文件中控件的摆放顺序为标准的

UIView *yellowview = [[NSBundel mainBundel] LoadNibName :@"yellowview" owner:nil options:nil ]lastObject];

 

2)获取xib文件中各项控件并对其进行赋值,可以根据每个控件的tag标志 viewWithTag

UIImageView *iconimage = (UIImageVIew*)[yellowview viewWithTag:10];

UILabel *namelabel = (UILabel*)[yellowview viewWithTag:20];

这种方法的缺陷:扩展性不好,而且要是控件数量多的时候代码量很大,控件暴露在外容易被修改,安全性低;

 

2.1)方法二 创建一个xib中view的类,继承自UIView,而不是默认使用UIView并对其进行关联

yellowview *yellowview= [[[NSBundle mainBundel]LoadNibName:@"yellowview" owner:nil options:nil]lastObject];

// 因为不能在引用类中直接关联控件数据,因为那样的话 各种控件还是暴露在外部,所以直接由外部传入控件数据

@classs Appmodel;

@property(nonatomic,strong)Appmodel *appmodel;

//在yellowview文件中对所有控件进行关联命名

-(void) setAppmodel:(Appmodel*)appmodel

{

_appmodel =appmodel;

_iconimage.image =[UIImage imageNamed:appmodel.icon];

_namelabel.text = appmodel.name;

}

 

4、点击加载时显示下载中并界面黑色透明view

 

1)设置view的透明度

UIView *controllerview =self.superview;

UIView *coverview =[[UIView alloc]initWithFrame:CGRectMake(0,0,200,200)];

[coverview setBackgroundColor :[UIColor blackColock]];

coverview.alpha = 0.6;

[controllerview addSubview :coverview];

2)怎么设置加载中界面的动画效果?

2.1)用block(虽然跳转了,但是跳转是瞬间完成的)

[UIView animateWithDuration :1 animations:^

{

coverview .alpha =0;

}];

2.2)分别设置两个block (但是其先后顺序是没有保证的,很可能会同时执行)

[UIView animateDuration:2 animations:^

{

cover.alpha=0.6;

}];

[UIView animateDuration:2 animations:^{

coverview .alpha =0;

}];

2.3)在animateWithDuration animations completion中嵌套block 发现内部变化中间没有停顿

[UIView animateDuration:1 animations:^

{

coverview.alpha=0.6;

}completion:^(BOOL finished)

{

//coverview.alpha =0;

//[UIView animateWithDuration:1 animations:^{ coverview.alpha=0;}];

}];

2.4)用animateWithDuration delay options animations completion中完成最后第二个参数方法中animations表示内部要执行的动画

[UIView animateWithDuration:1

delay:1

options: UIViewAnimationOptionCurveEaseInOut//选择实现效果

animations:^{ coverview.alpha =0}

completion:^(BOOL finisheed){//结束后实现

_download.enabled =NO;//将下载按钮变成enabled状态

}];

}];

5、 一些控件属性 CGReckGetMaxY:可以返回frame中最大的Y坐标,同理由CGRectGetMaxX,CGRectGetMinX,CGRectGetMinY

CGFloat labely =CGRectGetMaxX(iconview.frame);

文本居中:

namelabel.textAlignment = NSTextAlignmentCenter;

namelabel.center = CGPoint(200,200);

文本字体

namelabel.fonr = [UIFont systemFontOfSize:15];

systemFontOfSize/boldSystemFontOfSize/italicSystemFontOfSize/

按钮字体

btn.titleLabel.font= [UIFont systemFontOfSize:15];