在网上搜了好多对于做下拉菜单的代码,大部分都是做tableview的下拉菜单或者是抄袭,对于大牛们所说的简单类似于tableview的说法,我觉得没那么简单。琢磨了好久,还是用这个方法做了一个scrollview下拉菜单的例子,另外添加点击转跳用的是按钮转跳没有用cell事件注意了。
首先要创建一个uiview文件,设置好需要做的scrollview样板这里就给小菜们手打一个样板
.h文件里
#import <UIKit/UIKit.h> @class AEArtStudioController;//因为是交叉使用函数所以用@class来引用 @interface PullDownVIew : UIView<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> { AEArtStudioController *m_studio_control;//执行点击转跳的页面(文件名*函数名) } @property (strong, nonatomic)UICollectionView *collectionView; @property (nonatomic,strong) NSString *cellString; @property (nonatomic,retain) NSArray *scrollviewArray; -(id)initWithFrame:(CGRect)frame :(AEArtStudioController *)studio_control; @end
.m文件里
- (id)initWithFrame:(CGRect)frame :(AEArtStudioController *)studio_control { self = [super initWithFrame:frame]; if (self) { m_studio_control = studio_control;//执行转跳赋值 self.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.5]; [self collectionView_pull];//要执行的collectionview内容添加到本函数里 self.scrollviewArray = @[@"北京市", @"天津市",@"上海市",@"重庆市",@"黑龙江",@"吉林省",@"辽宁省",@"内蒙古",@"河北省",@"山东省",@"江苏省",@"浙江省",@"福建省",@"台湾省",@"广东省",@"江西省",@"湖南省",@"安徽省",@"湖北省",@"河南省",@"山西省",@"宁夏",@"陕西省",@"甘肃省",@"青海省",@"四川省",@"贵州省",@"云南省",@"广西",@"西藏",@"新疆"]; } return self; } //继续编写collectionView
-(void)collectionView_pull
{
//确定是水平滚动,还是垂直滚动
UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayoutalloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
//
self.collectionView=[[UICollectionViewalloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 330) collectionViewLayout:flowLayout];
self.collectionView.dataSource=self;
self.collectionView.delegate=self;
[self.collectionViewsetBackgroundColor:[UIColorcolorWithRed:250.0green:250.0blue:250.0alpha:0.9]];
//注册Cell,必须要有
[self.collectionViewregisterClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:@"UICollectionViewCell"];
[selfaddSubview:self.collectionView];
}
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return (_scrollviewArray.count);
// return [self.ViewControllerArry count];
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString * CellIdentifier = @"UICollectionViewCell";
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.backgroundColor = [UIColorclearColor];
UIButton *labelBtn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
// 按钮位置
labelBtn.frame = CGRectMake(0, 0, 85, 30);
[labelBtn setTitle:@""forState:UIControlStateNormal];
// 字体位置
[labelBtn setTitleEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 10)];
labelBtn.titleLabel.font = [UIFontsystemFontOfSize: 16.0];
[labelBtn setTitleColor:[UIColorgrayColor] forState:UIControlStateNormal];
labelBtn.backgroundColor = [UIColorwhiteColor];
// 设置Button圆角
[labelBtn.layersetMasksToBounds:YES];
[labelBtn.layersetCornerRadius:3.0]; //设置矩形四个圆角半径
[labelBtn addTarget:selfaction:@selector(jump_city) forControlEvents:UIControlEventTouchUpInside];
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(17, 2, 60, 30)];
label.textColor = [UIColorblackColor];
label.text =[self.scrollviewArrayobjectAtIndex:[indexPath row]];
label.backgroundColor =[UIColorcolorWithRed:221.0green:219.0blue:218.0alpha:0.7];
[labelBtn addSubview:label ];
[cell.contentViewaddSubview:labelBtn];
return cell;
}
//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
returnCGSizeMake(73, 30);
// 上下间距
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
returnUIEdgeInsetsMake(5, 20, 80,30);
// 上坐下有
}
-(void)jump_city//点击转跳翻页
{
AE_draw_info_right *infoLeft =[[AE_draw_info_rightalloc]init];
infoLeft.title =@"画室";
[m_studio_control.navigationControllerpushViewController:infoLeft animated:true];
}
@end
//点击事件的页面自己设置就可以了,大概思路就是在那个要展示的页面里的在新建的文件里的加入(.h文件里)
{PullDownVIew *m_pullDownView;}
- (void)viewDidLoad里创建一个按钮或label,然后在本函数viewDidLoad里设置一个下拉页面下拉位置
m_pullDownView = [[PullDownVIewalloc]initWithFrame:CGRectMake(0, -330, self.view.frame.size.width, 100) : self];
[self.view addSubview:m_pullDownView];
//注意是在(- (void)viewDidLoad里添加label)
//如果是按钮那就添加点击事件,如果是label那就添加手势动作。就不一一列举了,按钮最常见,那就举个label吧在-(void)viewDidLoad创建label。
UILabel *areaLabel = [[UILabelalloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-40, 0, 80, 40)];
areaLabel.backgroundColor = [UIColorclearColor];
areaLabel.textColor = [UIColorwhiteColor];
areaLabel.textAlignment = NSTextAlignmentCenter;
areaLabel.font = [UIFontsystemFontOfSize:20.f];
areaLabel.text = @"AAA";
[titleView addSubview:imageViewtitle];
[titleView addSubview:areaLabel];
//给label添加一个点击事件手势
UITapGestureRecognizer *tapGes = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapSelectProvinceView)];
[titleView addGestureRecognizer:tapGes];
}
//点击事件
- (void)tapSelectProvinceView
{
NSLog(@"tapSelectProvinceView");
[UIViewanimateWithDuration:0.2animations:^{
if(m_pullDownView.frame.origin.y == -330)
{
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
m_pullDownView.frame = frame;
}
elseif(m_pullDownView.frame.origin.y == 0) {
CGRect frame = CGRectMake(0, -330, self.view.frame.size.width, 100);
m_pullDownView.frame = frame;
}
}];