LeftMenu
包含文件
- LeftMenuViewController.h, LeftMenuViewController.m
- TMItemView.h, TMItemView.m
- ThemeItemModel.h, ThemeItemModel.m
实现功能
1、利用xib实现大体UI框架
2、在viewDidLoad调用getThemeList
- 向http://news-at.zhihu.com/api/4/themes获取数据, 并填充到_subscribedList和_othersList中。
- 加载数据完成后,调用setMentItems
- (void)getThemeList {
[HttpOperation getRequestWithURL:@"themes" parameters:nil success:^(id responseObject) {
NSDictionary *jsonDic = (NSDictionary*)responseObject;
_subscribedList = [NSMutableArray array];
NSArray *subArr = jsonDic[@"subscribed"];
for (NSDictionary *dic in subArr) {
ThemeItemModel *model = [[ThemeItemModel alloc] initWithDictionary:dic];
[_subscribedList addObject:model];
}
_othersList = [NSMutableArray array];
NSArray *othArr = jsonDic[@"others"];
for (NSDictionary *dic in othArr) {
ThemeItemModel *model = [[ThemeItemModel alloc] initWithDictionary:dic];
[_othersList addObject:model];
}
[self setMentItems]; //完成UI的设置
} failure:nil];
}
3、设置UI
- "TMItemView"的xib如下图
- 设置scrollview的内容视图大小为:1(首页)+ 订阅(_subscribedList) + 其他(_othersList)
- 设置Home Item,frame为(x=0,y=0, scrollview的宽,高度为44),名字为"首页",图片为"Menu_Icon_Home",添加tap手势,手势函数为“showHome”,将控件添加到_mainScrollView
- 设置_subscribedList和_othersList数组,创建对应个数的themeItems和TMItemView,并配置参数,添加手势函数didSelectedMenuItem
- (void)setMentItems {
_mainScrollView.contentSize = CGSizeMake(0, (1+_subscribedList.count+_othersList.count)*44.f);
//设置Home Item
TMItemView* homeItem = [[[NSBundle mainBundle] loadNibNamed:@"TMItemView" owner:self options:nil] firstObject];
homeItem.frame = CGRectMake(0, 0, _mainScrollView.width, 44);
homeItem.menuTitleLb.text = @"首页";
homeItem.menuImaView.image = [UIImage imageNamed:@"Menu_Icon_Home"];
[homeItem addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showHome:)]];
[_mainScrollView addSubview:homeItem];
CGFloat tempHeight = homeItem.height;
NSMutableArray *themeItems = [NSMutableArray arrayWithArray:_subscribedList];
[themeItems addObjectsFromArray:_othersList];
for (int i = 0; i<themeItems.count; i++) {
TMItemView* itemView = [[[NSBundle mainBundle] loadNibNamed:@"TMItemView" owner:self options:nil] firstObject];
itemView.frame = CGRectMake(0, tempHeight, _mainScrollView.width, 44);
[itemView.menuImaView removeFromSuperview];
[itemView addConstraint:[NSLayoutConstraint constraintWithItem:itemView.menuTitleLb attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:itemView attribute:NSLayoutAttributeLeading multiplier:1 constant:4]];
ThemeItemModel *model = themeItems[i];
itemView.menuTitleLb.text = model.name;
[itemView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didSelectedMenuItem:)]];
[itemView setTag:i];
[_mainScrollView addSubview:itemView];
tempHeight += 44.f;
}
_themeItems = themeItems;
}
4、 HomeItem的手势
- 返回MainViewController
- (void)showHome:(UIGestureRecognizer *)recognizer {
AppDelegate *appdele = kAppdelegate;
[appdele.mainVC showMainView];
}
5、ThemeItem的手势
- 使用presentViewController方式跳转到DailyThemesViewController
- (void)didSelectedMenuItem:(UIGestureRecognizer *)recognizer {
ThemeItemModel *model = _themeItems[recognizer.view.tag];
DailyThemesViewController *dailyThemeVC = [[DailyThemesViewController alloc] init];
dailyThemeVC.themeID = model.themeID;
UINavigationController *subNavigationVC = [[UINavigationController alloc] initWithRootViewController:dailyThemeVC];
AppDelegate* appdele = kAppdelegate;
[appdele.mainVC presentViewController:subNavigationVC animated:YES completion:nil];
}