内存优化之封装九宫格

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

随着市场上越来越多的APP上线,好多软件对手机的内存要求也是很大,所以我们在开发的时候一定要掌握如何去优化内存,将自己的APP尽可能优化。今天我们就一起看一下九宫格的优化。下面是软件的截图

1、为了达到更好的效果我们不用UITableView,首先我们要通过XIB自定义一个图片和文字如图:

2、自定义主视图JRMainScrollView,通过协议代理来实现功能,做法和UITableView类似,大家可以参考一下UITableView

首先:我们要定义数据源协议

//数据源协议@PRotocolJRMainScrollDataSource <NSObject>//获取总的数量-(NSInteger) numberOfItems;//获取列的数量-(NSInteger) numberOfColumsOfRow;//获取item- (UIView *) mainScrollView:(JRMainScrollView *)mainScrollView itemAtIndex:(NSInteger) index;@end

其次:我们要定义属性协议

//属性协议@protocolJRMainScrollDelegate <NSObject>@optional//获取高度- (CGFloat) heightForItemAtView:(JRMainScrollView *) mainScrollView;//获取宽度- (CGFloat) widthForItemAtView:(JRMainScrollView *) mainScrollView;//获取间距- (CGFloat) mainScrollView:(JRMainScrollView *)mainScrollView spaceForItemWithType:(kJRMainScrollItemSpace)type;@end

注意获取间距包括到左右的间距和上下的间距通过定义一个枚举实现

typedefenum{

kJRMainScrollItemLeftSpace,

kJRMainScrollItemTopSpace

} kJRMainScrollItemSpace;

3、内部布局实现,计算出当前所有的frame,并且放入数组在此期间,用的的属性参数都需要从代理来获取,代码如下

//加载子视图- (void)_loadSubViews{//获取总个数和列数NSInteger totalItems=[self.jrDataSource numberOfItems];

NSInteger colum=[self.jrDataSource numberOfColumsOfRow];//获取宽度和高度CGFloat itemWidth=[self.jrDelegate widthForItemAtView:self];

CGFloat itemHeigt=[self.jrDelegate heightForItemAtView:self];//获取上下间距CGFloat leftSpace=[self.jrDelegate mainScrollView:self spaceForItemWithType:kJRMainScrollItemLeftSpace];

CGFloat topSpace=[self.jrDelegate mainScrollView:self spaceForItemWithType:kJRMainScrollItemTopSpace];

CGFloat space=(kWidth-2*leftSpace-colum*itemWidth)/(colum-1)+itemWidth;for(inti=0;i<totalItems;i++) {intclo=i%colum;introw=i/colum;

CGRect frame=CGRectMake(leftSpace+clo*space,20+row*(itemHeigt+topSpace), itemWidth, itemHeigt);

[self.array addObject:[NSValue valueWithCGRect:frame]];

}

self.contentSize=CGSizeMake(0, CGRectGetMaxY([[self.array lastObject] CGRectValue]));

self.showsVerticalScrollIndicator=NO;

}

4、判断当前的frame是不是在当前的屏幕可视范围之内,如果要是在的进行视图的渲染,如果不在不予理睬。

-(void)layoutSubviews{

[super layoutSubviews];//循环便利获取在屏幕中的framefor(inti=0;i<self.array.count;i++) {

UIView* tempView=(UIView *)self.current[@(i)];

CGRect rect=[self.array[i] CGRectValue];if([self isInScreenWith:rect]) {if(!tempView){//字典里没有的才需要重重新加载UIView *view=[self.jrDataSource mainScrollView:self itemAtIndex:i];

view.frame=rect;

[self.current setObject:view forKey:@(i)];

[self addSubview:view];

}

}elseif(tempView){//如果存在字典而且不在视线内部的则移除[self.current removeObjectForKey:@(i)];

[tempView removeFromSuperview];

[self.pool addObject:tempView];

}

}//判断是不是在视野内部,其中有两种情况,Y值在屏幕内部,或者MAXY值在屏幕内部-(BOOL) isInScreenWith:(CGRect) frame{

CGFloat setMiny=self.contentOffset.y;

CGFloat setMaxy=self.contentOffset.y+kHeight;

BOOL condition1=frame.origin.y>=setMiny&&frame.origin.y<=setMaxy;

BOOL condition2=CGRectGetMaxY(frame)>=setMiny&&CGRectGetMaxY(frame)<=setMaxy;if(condition1||condition2){returnYES;

}returnNO;

}

5、操作缓冲池重复利用对象

/** 存放frame*/@property(nonatomic,strong) NSMutableArray*array;/** 存放当前显示的对象*/@property(nonatomic,strong) NSMutableDictionary*current;/** 存放缓冲池对象*/@property(nonatomic,strong) NSMutableSet*pool;/**

* 获取重复利用的对象

*

* @param identy <#identy description#>

*

* @return <#return value description#>*/- (JRRectView *) dequeueReusedItemWithIdenty:(NSString *) identy{

JRRectView* view=[self.pool anyObject];if(view!=nil) {

[self.pool removeObject:view];

}returnview;

}

6、在主控制器加载视图并实现代理方法即可

//加载所有数据- (void) _loadSubviews{//1 增加滚动视图JRMainScrollView * mainScroll=[[JRMainScrollView alloc] initWithFrame:self.view.bounds];

mainScroll.jrDataSource=self;

mainScroll.jrDelegate=self;

[mainScroll reloadViews];

[self.view addSubview:mainScroll];

}#pragmamark - 数据源方法

-(NSInteger)numberOfItems{return132;

}-(NSInteger) numberOfColumsOfRow{return3;

}-(UIView *) mainScrollView:(JRMainScrollView *)mainScrollView itemAtIndex:(NSInteger)index{

JRRectView*cell=[mainScrollView dequeueReusedItemWithIdenty:@"test"];if(cell==nil) {

cell=[[[NSBundle mainBundle] loadNibNamed:@"rect"owner:nil options:nil] lastObject];

}

cell.titleLabel.text=[NSString stringWithFormat:@"下载"];

NSString* imageName=[NSString stringWithFormat:@"%d",arc4random_uniform(20)+256];

UIImage*image=[UIImage imageNamed:imageName];

cell.image.image=image;returncell;

}#pragmamark - 代理方法//获取高度- (CGFloat) heightForItemAtView:(JRMainScrollView *) mainScrollView{return100;

}//获取宽度- (CGFloat) widthForItemAtView:(JRMainScrollView *) mainScrollView{return90;

}//获取间距- (CGFloat) mainScrollView:(JRMainScrollView *)mainScrollView spaceForItemWithType:(kJRMainScrollItemSpace)type{if(type==kJRMainScrollItemLeftSpace) {return20;

}elseif(type==kJRMainScrollItemTopSpace){return20;

}return20;

}

想要了解更多内容的小伙伴,可以点击查看源码,亲自运行测试。

疑问咨询或技术交流,请加入官方QQ群: (452379712)

作者:杰瑞教育

出处:http://www.cnblogs.com/jerehedu/

本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航