学习制作iOS程序第四天:工具类、网络请求类和首页之下拉放大图,首页之图标菜单(15~17)
前置工作好像做的差不多了,是不是该干点正事了?
十五:创建ToolsHelper和HttpHelper
这两个文件分别是工具类和网络请求类。具体代码就不贴了。贴下h文件吧。
#import <Foundation/Foundation.h> @interface ToolsHelper : NSObject //单例模式 +(instancetype)toolsHelper; #pragma mark - 弹出HUD提示 -(void)ShowHUDWithTitle:(NSString *)title andSeconds:(NSInteger)time; -(void)ShowHUDWithTitle:(NSString *)title; -(void)CloseHUD; #pragma mark - 根据字节返回大小 -(NSString *)fileSizeWithIntege:(NSInteger)size; @end
#import <Foundation/Foundation.h> //代理方法 @protocol HttpHelperDelegate <NSObject> -(void)HttpExecuteSuccess:(AFHTTPRequestOperation *)operation andResponseObject:(id)responseObject andFlag:(NSString *)flag; -(void)HttpExecuteFailure:(AFHTTPRequestOperation *)operation andError:(NSError *)error andFlag:(NSString *)flag; @end @interface HttpHelper : NSObject @property(nonatomic,weak) id<HttpHelperDelegate>delegate; -(HttpHelper *)initHttpHelper; +(instancetype)httpHelper; #pragma mark - 取消网络请求 -(void)CancelAllRequest; #pragma mark - 通用get/post请求提交 -(void)getDataWithURLString:(NSString *)urlString andParameters:(NSMutableDictionary *)dict andHUD:(BOOL)ishud andFlag:(NSString *)flag; -(void)postDataWithURLString:(NSString *)urlString andParameters:(NSMutableDictionary *)dict andHUD:(BOOL)ishud andFlag:(NSString *)flag; @end
十六:首页之一 - 宣传图片(关键字,下拉图片放大)
参考网站:http://jingyan.baidu.com/album/fdffd1f85a3a4af3e98ca13c.html?picindex=1,他用的tableview我用的scrollview
.h文件代码
@property(strong,nonatomic) UIScrollView *scrollview;
@property(strong,nonatomic) UIImageView *zoomImageView;
.m文件代码的viewDidload方法
//页面标题 self.title=@"首页"; self.navigationController.navigationBar.hidden=YES; //初始化scrollview self.scrollview =[[UIScrollView alloc] initWithFrame:self.view.bounds]; self.scrollview.contentInset = UIEdgeInsetsMake(MAINBGHEIGHT, 0, 0, 0); self.scrollview.delegate = self; self.scrollview.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT); [self.view addSubview:_scrollview]; //初始化缩放图片 self.zoomImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, -MAINBGHEIGHT, SCREEN_WIDTH, MAINBGHEIGHT)]; self.zoomImageView.image = [UIImage imageNamed:@"main_bg"]; self.zoomImageView.contentMode = UIViewContentModeScaleAspectFill; [self.scrollview addSubview:_zoomImageView];
#pragma mark scrollview滚动事件 -(void)scrollViewDidScroll:(UIScrollView *)scrollView { //当滚动的时候缩放图片 CGFloat y = _scrollview.contentOffset.y; if (y<=200) { CGRect frame=_zoomImageView.frame; frame.origin.y = y; frame.size.height = -y; _zoomImageView.frame = frame; } }
十七:首页之三 - 图标导航
一共有8个导航图标,为了精确排版,通过循环遍历来处理图片和文字的展示。
1、定义变量
NSArray *arrMenuImage;
NSArray *arrMenuText;
2、在viewDidload方法中初始化数据
//初始化八个图片导航信息 arrMenuImage = [NSArray arrayWithObjects:@"menu_project",@"menu_sale",@"menu_rent",@"menu_agent",@"menu_maps",@"menu_jisuanqi",@"menu_news",@"menu_tools",nil]; arrMenuText=[NSArray arrayWithObjects:@"新房",@"二手房",@"租房",@"经纪人",@"地图找房",@"计算器",@"新闻资讯",@"实用工具",nil]; [self initMenu];
3、初始化8个图标
#pragma mark 初始化8个图标 -(void)initMenu { CGFloat imgwidth = 46.0f; CGFloat imgpadding = (SCREEN_WIDTH - imgwidth*4)/5; for (int i=1; i<=arrMenuImage.count; i++) { int row = ceil((i-0.1f)/4); int col = i%4; if(col==0){col=4;} UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame=CGRectMake(imgpadding+(col-1)*(imgwidth+imgpadding), 15*row+(row-1)*(imgwidth+15), imgwidth, imgwidth); btn.tag=i; [btn setImage:[UIImage imageNamed:arrMenuImage[i-1]] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [_scrollview addSubview:btn]; UILabel *lbl=[[UILabel alloc] init]; lbl.text=arrMenuText[i-1]; lbl.frame=CGRectMake(imgpadding/2 + (col-1)*((SCREEN_WIDTH-imgpadding)/4), btn.frame.origin.y+imgwidth, (SCREEN_WIDTH-imgpadding)/4, 25); lbl.textAlignment=NSTextAlignmentCenter; lbl.font=[UIFont systemFontOfSize:12]; [_scrollview addSubview:lbl]; } }
4、点击菜单事件,先用alert占位。
#pragma mark 点击菜单的事件 -(void)btnClick:(UIButton *)btn { switch (btn.tag) { case 1://新房 [[ToolsHelper toolsHelper] ShowAlertWithTitle:@"开发中..." andContent:@"此功能正在开发中,敬请期待"]; break; case 2://二手房 [[ToolsHelper toolsHelper] ShowAlertWithTitle:@"开发中..." andContent:@"此功能正在开发中,敬请期待"]; break; case 3://租房 [[ToolsHelper toolsHelper] ShowAlertWithTitle:@"开发中..." andContent:@"此功能正在开发中,敬请期待"]; break; case 4://经纪人 [[ToolsHelper toolsHelper] ShowAlertWithTitle:@"开发中..." andContent:@"此功能正在开发中,敬请期待"]; break; case 5://地图找房 [[ToolsHelper toolsHelper] ShowAlertWithTitle:@"开发中..." andContent:@"此功能正在开发中,敬请期待"]; break; case 6://计算器 [[ToolsHelper toolsHelper] ShowAlertWithTitle:@"开发中..." andContent:@"此功能正在开发中,敬请期待"]; break; case 7://新闻资讯 [[ToolsHelper toolsHelper] ShowAlertWithTitle:@"开发中..." andContent:@"此功能正在开发中,敬请期待"]; break; case 8://实用工具 [[ToolsHelper toolsHelper] ShowAlertWithTitle:@"开发中..." andContent:@"此功能正在开发中,敬请期待"]; break; default: break; } }
最后记录一个问题。如何消除第三方组件的ARC错误或者其他警告?
作为一个非处女座的完美主义者,编译的时候出现好多的警告实在不能忍受。
1、进入Targets,Build Phases,找到Complie Source下的相应的报错文件
2、在Compiler Flags中加入“-fno-objc-arc”可以去掉ARC警告
3、加入“-w”可以去除所有警告