IOS 一个仿美团导航(分类)
来源:互联网 发布:东方财富网python面试 编辑:程序博客网 时间:2024/06/06 05:44
前言:仿美团导航,先来张效果团。
代码调用:
NSArray *bcImageArray = [NSArray arrayWithObjects:@"b_cup",@"b_book",@"b_bag",@"b_fruit",@"b_ball",@"b_tool",@"b_lipstick",@"b_hotel",@"b_clock",@"b_shopping_car",@"b_question",@"b_all", nil];NSArray *bcTitleArray = [NSArray arrayWithObjects:@"餐饮美食",@"教育培训",@"休闲娱乐",@"地方特产",@"运动健身",@"技术服务",@"时尚丽人",@"酒店住房",@"居家生活",@"海外代购",@"咨询服务",@"全部",nil];CGRect bcFrame = CGRectMake(0, 10, self.view.frame.size.width, 160);IWBusinessCategorySV *businessCategorySV = [IWBusinessCategorySV businessCategorySVWithFrame:bcFrame imageArray:bcImageArray titleArray:bcTitleArray target:self action:@selector(clickBusinessCategoryBtn:)];[self.view addSubview:businessCategorySV];
正常我们是将这个View添加tableview的头部。
代码实现:
#import <UIKit/UIKit.h>UIKIT_EXTERN const NSInteger KCategoryTag;@interface IWBusinessCategorySV : UIView/** 实例化 @param frame frame @param imageArray 图片数组 @param titleArray 标题数组 @param target target @param action 分类按钮点击事件 @return 分类视图 */+ (IWBusinessCategorySV *)businessCategorySVWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray titleArray:(NSArray *)titleArray target:(id)target action:(SEL)action;@end
#import "IWBusinessCategorySV.h"#import "IWItemButton.h"const NSInteger KCategoryTag = 100;static const NSInteger KPageCount = 8;//一页的个数static const NSInteger KPageSection = 2;//一页的行数@implementation IWBusinessCategorySV- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { //1.设置背景颜色 self.backgroundColor = [UIColor whiteColor]; } return self;}+ (id)businessCategorySV{ return [[self alloc] init];}+ (IWBusinessCategorySV *)businessCategorySVWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray titleArray:(NSArray *)titleArray target:(id)target action:(SEL)action{ IWBusinessCategorySV *businessCategorySV = [self businessCategorySV]; businessCategorySV.frame = frame; //1.添加UIScrollView UIScrollView *categoryScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; categoryScrollView.pagingEnabled = YES; categoryScrollView.showsHorizontalScrollIndicator = NO; categoryScrollView.showsVerticalScrollIndicator = NO; NSInteger page = (titleArray.count + KPageCount - 1)/KPageCount; categoryScrollView.contentSize = CGSizeMake(frame.size.width * page, frame.size.height); [businessCategorySV addSubview:categoryScrollView]; //2.添加button CGFloat itemBtnW = frame.size.width/(KPageCount/KPageSection); CGFloat itemBtnH = frame.size.height/KPageSection; for (int i = 0; i < titleArray.count; i ++) { IWItemButton *itemBtn = [IWItemButton buttonWithType:UIButtonTypeCustom]; itemBtn.tag = i + KCategoryTag; [itemBtn setImage:[UIImage imageNamed:imageArray[i]] forState:UIControlStateNormal]; [itemBtn setTitle:titleArray[i] forState:UIControlStateNormal]; [itemBtn setTitleColor:[UIColor colorWithRed:99/255.0 green:99/255.0 blue:99/255.0 alpha:1.0] forState:UIControlStateNormal]; CGFloat itemBtnX = i/KPageCount *frame.size.width + (i%(KPageCount/KPageSection))*itemBtnW; CGFloat itemBtnY = ((i - (i/KPageCount)*KPageCount)/(KPageCount/KPageSection))*itemBtnH; itemBtn.frame = CGRectMake(itemBtnX, itemBtnY, itemBtnW, itemBtnH); [itemBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; [categoryScrollView addSubview:itemBtn]; } return businessCategorySV;}@end
按钮子类
#import <UIKit/UIKit.h>@interface IWItemButton : UIButton@end
#import "IWItemButton.h"// 文字的高度比例static const CGFloat kTitleRatio = 0.4;@implementation IWItemButton- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { //1.设置文字居中 self.titleLabel.textAlignment = NSTextAlignmentCenter; //2.设置文字大小和颜色 self.titleLabel.font = [UIFont systemFontOfSize:12.0f]; [self setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal]; //3.设置图片居中 self.imageView.contentMode = UIViewContentModeScaleAspectFit; } return self;}#pragma mark 覆盖父类的方法- (void)setHighlighted:(BOOL)highlighted{}#pragma mark - 设置图片和文字居中- (CGRect)imageRectForContentRect:(CGRect)contentRect{ CGFloat imageX = 0; CGFloat imageY = contentRect.size.height*0.1; CGFloat imageW = contentRect.size.width; CGFloat imageH = contentRect.size.height * (1 - kTitleRatio) - imageY; return CGRectMake(imageX, imageY, imageW, imageH);}- (CGRect)titleRectForContentRect:(CGRect)contentRect{ CGFloat titleX = 0; CGFloat titleH = contentRect.size.height * kTitleRatio; CGFloat titleY = contentRect.size.height - titleH; CGFloat titleW = contentRect.size.width; return CGRectMake(titleX, titleY, titleW, titleH);}@end
demo下载:http://download.csdn.net/detail/u011154007/9667869
1 0
- IOS 一个仿美团导航(分类)
- 【导航条滚动透明】一个分类搞定
- 【导航条滚动透明】一个分类搞定
- iOS中创建一个分类
- iOS 创建一个滑出式导航面板
- iOS 分类(category)
- iOS- 自定义导航控制器(-)
- 地图导航ios(url)
- iOS开发 自定义一个全屏的蒙板(导航栏也能遮挡住)
- iOS开发 自定义一个全屏的蒙板(导航栏也能遮挡住)
- 完善商品分类导航
- 分类连接导航
- wordpress 分类导航原理
- 完善商品分类导航
- 导航内容分类
- 商城分类导航效果
- 分类导航数据杂谈
- 商场分类导航效果
- IAR 6.30.1下载链接 及其破解文件
- 写给Android开发者的混淆使用手册
- 进程调度函数schedule()分析
- 序列检测器
- Android中常用布局的使用
- IOS 一个仿美团导航(分类)
- Oracle-分区表解读
- 题目:输入5个数(含负数、小数)将它们按由小到大的顺序排列起来 提示:需要排数的数字通过参数传递进来, 例如:输入:./a.out -1 2.1 -3 5 7 输出: -3 -1 2.1
- 如何设计更好的在线教育直播推流方案
- java高级特性-----注解(Annotation)
- 线程创建、等待、终止
- _onexit函数的使用 完成主函数执行完毕后再执行一段程序
- java中的“宏定义”
- linux 重启网卡 Device eth0 has different MAC address than expected,