欢迎使用CSDN-markdown编辑器
来源:互联网 发布:mmap for mac 编辑:程序博客网 时间:2024/05/21 05:56
项目:疯狂食材的总结与心得
疯狂食材是一个比较简单的项目,界面简洁清新。所以我依样画葫芦,模仿它的界面,自己做了一遍。
首页
由于系统的UITabBar不能满足我的需求,所以要自定义一个bar。也就是一个View代替UITabBar。
@interface BarViewController (){ UIView *customTabBar; UIButton *temButton;}
- (void)viewDidLoad { [super viewDidLoad]; [self.tabBar setHidden:YES]; //初始当前的页面 self.selectedIndex = 0; [self initCustomTarBar];}
- (void)initCustomTarBar{ customTabBar = [[UIView alloc] initWithFrame:CGRectMake(0, UIScreen_Height - 44, UIScreen_Width, 44)]; customTabBar.backgroundColor = [UIColor whiteColor]; [self.view addSubview:customTabBar]; NSArray *normalArray = @[[UIImage imageNamed:@"Category"],[UIImage imageNamed:@"Crowd"],[UIImage imageNamed:@"Efficacy"],[UIImage imageNamed:@"Nutrition"],[UIImage imageNamed:@"More"]]; for (int i = 0; i < self.viewControllers.count; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.tag = i; CGFloat space = (UIScreen_Width - 5*30)/6; button.frame = CGRectMake(space*(i+1) + 30*i, 7, 30, 30); [button setImage:normalArray[i] forState:0]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [customTabBar addSubview:button]; }}- (void)buttonAction:(UIButton *)sender{ if (sender == temButton) { return; } sender.selected = YES; temButton.selected = !temButton.selected; self.selectedIndex = sender.tag;}
内容
类目的页面,就是个TableView。不过有个问提,最好不要用系统的,效果非常不好!!
人群的页面。这上面是个Label,不过他的高度要根据文本算高度。 代码如下:
CGRect rect = [string boundingRectWithSize:CGSizeMake(UIScreen_Width - 44,1800) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil]; CGSize size = rect.size;
营养页面,也是个tableView。这里有个问题,上面的那个view不要用此方法:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{}
用了之后你会发现这个view会是你滑动跟着动,会跟着显示....因此,如果只是要得到第一个表头,就要用`self.tableView.tableHeaderView = View;`其中的view就是你的表头内容。
食材详情页面,这应该是这个项目最有难度的地方了吧。这里要分很多风分区,不同的分区用不同的cell.
- (void)initHeader{//设置表头 UIView *View = [[UIView alloc] initWithFrame:CGRectMake(0,0, UIScreen_Width,290)]; [View setContentMode:UIViewContentModeScaleAspectFill]; UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, UIScreen_Width, 250)]; [imageView sd_setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"moon"]]; [View addSubview:imageView]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 250, UIScreen_Width, 40)]; label.text = [NSString stringWithFormat:@" 营养价值: (%@)",per]; label.textColor = [UIColor redColor]; label.backgroundColor = [UIColor colorWithRed:253/255.0 green:227/255.0 blue:198/255.0 alpha:1]; [View addSubview:label]; self.tableView.tableHeaderView = View;}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 6;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { return contentArray.count; }else if (section == 1 || section == 3) { return 1; }else if (section == 2) { return goodArray.count; }else if (section == 4) { return badArray.count; }if (section == 5) { return desArray.count; } return 1;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { //营养价值的table ATableViewCell *acell = [tableView dequeueReusableCellWithIdentifier:Acell forIndexPath:indexPath]; acell.nameLabel.text = contentArray[indexPath.row][@"key"]; acell.VaLabel.text = [NSString stringWithFormat:@"%@%@",contentArray[indexPath.row][@"value"],contentArray[indexPath.row][@"unit"]]; return acell; }if (indexPath.section == 1 && goodArray.count != 0) { //食物搭配的宜表头 BTableViewCell *bcell = [tableView dequeueReusableCellWithIdentifier:Bcell forIndexPath:indexPath]; bcell.backgroundColor = [UIColor colorWithRed:253/255.0 green:227/255.0 blue:198/255.0 alpha:1]; bcell.imageV.image = [UIImage imageNamed:@"good1"]; return bcell; }if (indexPath.section == 3 && badArray.count != 0) { //食物搭配忌的表头 BTableViewCell *bcell = [tableView dequeueReusableCellWithIdentifier:Bcell forIndexPath:indexPath]; bcell.backgroundColor = [UIColor colorWithRed:253/255.0 green:227/255.0 blue:198/255.0 alpha:1]; bcell.imageV.image = [UIImage imageNamed:@"bad1"]; return bcell; }if (indexPath.section == 2 || indexPath.section == 4) { //食物搭配的宜、忌的内容 CTableViewCell *ccel = [tableView dequeueReusableCellWithIdentifier:Ccell forIndexPath:indexPath]; if (indexPath.section == 2) { [ccel.imageV sd_setImageWithURL:[NSURL URLWithString:goodArray[indexPath.row][@"pic"]] placeholderImage:[UIImage imageNamed:@"moon"]]; ccel.nameLabel.text = goodArray[indexPath.row][@"name"]; ccel.desLabel.text = goodArray[indexPath.row][@"description"]; ccel.desLabel.numberOfLines = 0; }if (indexPath.section == 4) { [ccel.imageV sd_setImageWithURL:[NSURL URLWithString:badArray[indexPath.row][@"pic"]] placeholderImage:[UIImage imageNamed:@"moon"]]; ccel.nameLabel.text = badArray[indexPath.row][@"name"]; ccel.desLabel.text = badArray[indexPath.row][@"description"]; ccel.desLabel.numberOfLines = 0; } return ccel; }if (indexPath.section == 5) { //文本内容 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, UIScreen_Width - 5, 30)]; titleLabel.text = desArray[indexPath.row][@"title"]; titleLabel.textColor = [UIColor redColor]; titleLabel.font = [UIFont systemFontOfSize:14]; [cell addSubview:titleLabel]; NSArray *arr = [NSArray arrayWithArray:desArray[indexPath.row][@"content"]]; for ( int i = 0; i < arr.count; i++) { UILabel *label = [[UILabel alloc] init]; label.frame = CGRectMake(5, 30 + space , UIScreen_Width - 10,[self getHeight:arr[i]]); space = space + [self getHeight:arr[i]]; label.text = [NSString stringWithFormat:@" %@",arr[i]]; label.font = [UIFont systemFontOfSize:14]; label.numberOfLines = 0; [cell addSubview:label]; } space = 0; return cell; } UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil]; return cell;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section == 0) { return 30; }else if (indexPath.section == 1) { if (goodArray.count == 0) { return 0; } return 44; }else if (indexPath.section == 3){ if (badArray.count == 0) { return 0; } return 44; }else if (indexPath.section == 2 || indexPath.section == 4) { return 80; }else if (indexPath.section == 5){ CGFloat height = 0.0 ; NSArray *arr = [NSArray arrayWithArray:desArray[indexPath.row][@"content"]]; for (int i = 0; i < arr.count; i++) { height = height + [self getHeight:arr[i]]; } return height + 40; } return 1;}- (CGFloat)getHeight:(NSString *)string{ CGRect rect = [string boundingRectWithSize:CGSizeMake(UIScreen_Width - 10,300) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil]; CGSize size = rect.size; return size.height;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section == 0) { [NetWork netWorkWithPath:@"getCategories2.aspx" Params:[NSDictionary dictionary] CallBack:^(NSDictionary *info, NetType type) { array = info[@"data"]; for (NSDictionary *ele in array[3][@"items"]) if ([contentArray[indexPath.row][@"key"] isEqualToString:ele[@"name"]]) { NutritionTableViewController *NTVC = [[NutritionTableViewController alloc] init]; NTVC.my_id = [ele[@"id"] intValue]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:NTVC]; [self presentViewController:nav animated:YES completion:nil]; } }]; }}
搜索
这里用的是一个第三方DBSphereTagCloud实现3D球形标签云效果。可以自动惯性滚动。
3D球形标签云效果
使用方法:
创建DBSphereView对象:
DBSphereView *view = [[DBSphereView alloc] initWithFrame:CGRectMake(0, 100, 320, 320)];
[view setCloudTags:buttonArray];
[self.view addSubView:view];
最后
介绍一些小技巧:
- 截图 :
UIGraphicsBeginImageContext(self.view.frame.size); //currentView 当前的view [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); NSString *path_sandox = NSHomeDirectory(); NSLog(@"%@",path_sandox); //设置一个图片的存储路径 NSString *imagePath = [path_sandox stringByAppendingString:@"/Documents/food.png"]; //把图片直接保存到指定的路径(同时应该把图片的路径imagePath存起来,下次就可以直接用来取) [UIImagePNGRepresentation(viewImage) writeToFile:imagePath atomically:YES];
2.清理缓存:
这个项目的网络请求的图片都是用《SDWebImage》请求的,就会产生缓存,清除缓存的方法如下:
[[[SDWebImageManager sharedManager] imageCache] clearDisk]; [[[SDWebImageManager sharedManager] imageCache] clearMemory]; [[NSURLCache sharedURLCache] removeAllCachedResponses]; [[[UIAlertView alloc] initWithTitle:@"清理缓存" message:@"你的缓存已经清理完毕" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show]; [SDWebImageManager.sharedManager.imageCache calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) { //计算缓存的大小 NSLog(@"%ld---------%ld",fileCount,totalSize); }];
0 0
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 生成ID的线程池
- oracle笔记第8周
- GridView的模板列如何编辑
- 第7周 项目3 - 负数把正数赶出队列
- WAYS TO UPDATE YOUR DEVICE
- 欢迎使用CSDN-markdown编辑器
- 第8周—项目1 建立顺序串的算法库
- Beyond Compare integrate with WinSCP & CVS
- 设备管理成功经验总结
- alm应用程序生命周期管理
- OC-----------Block块的注意点
- Hadoop学习,第一篇
- 关于/dev/fd/
- 一、ios的通过Xib构建UICollectionView的简单运用