iOS - 菜单栏menu选择控件
2017-02-15 11:08 菜鸟Alex 阅读(2798) 评论(0) 编辑 收藏 举报-
看图
-
代码
//
// ViewController.m
// test
//
// Created by 裴波波 on 2017/2/15.
// Copyright © 2017年 裴波波. All rights reserved.
//
#import "ViewController.h"
#define KScreen_Bounds [UIScreen mainScreen].bounds
#define KScreen_Size [UIScreen mainScreen].bounds.size
#define KScreen_Width [UIScreen mainScreen].bounds.size.width
#define KScreen_Height [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
//临时lineView 与 临时 btn
@property (strong, nonatomic) UIView * lineViewTemp;
@property (strong, nonatomic) UIButton * btnTemp;
//lineView数组
@property (strong, nonatomic) NSMutableArray * arrMLineView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.arrMLineView = @[].mutableCopy;
[self setLablesWithArrOfTitle:@[@"one",@"two",@"three"] andLeftDistance:0 andItWidth:KScreen_Width/3 andItHeight:35 andYcoordinate:64 andDestiView:self.view];
}
-(void)setLablesWithArrOfTitle:(NSArray<NSString *> *)arrTitles andLeftDistance:(CGFloat)lDistance andItWidth:(CGFloat)width andItHeight:(CGFloat)height andYcoordinate:(CGFloat) y andDestiView:(UIView *)destiView{
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat margin = (screenWidth - 2*lDistance - width*arrTitles.count) / (arrTitles.count - 1);
UIButton * tempLabel = [UIButton new];
for (int i = 0; i< arrTitles.count; i++) {
if (i == 0) {
UIButton * lbl = [[UIButton alloc] initWithFrame:CGRectMake(lDistance, y, width, height)];
lbl.tag = i;
[lbl setBackgroundColor:[UIColor orangeColor]];
[lbl setTitle:arrTitles[0] forState:UIControlStateNormal];
[destiView addSubview:lbl];
//line
UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, height - 5+64, width, 5)];
[self.arrMLineView addObject:lineView];
//设置默认第一个选中
lineView.backgroundColor = [UIColor redColor];
[lbl setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
_btnTemp = lbl;
_lineViewTemp = lineView;
[destiView addSubview:lineView];
lineView.hidden = NO;
tempLabel = lbl;
//绑定点击事件
[lbl addTarget:self action:@selector(clickBtnOne:) forControlEvents:UIControlEventTouchUpInside];
continue;
}
UIButton * lbl = [[UIButton alloc] initWithFrame:CGRectMake((CGRectGetMaxX(tempLabel.frame)+margin), y, width, height)];
lbl.tag = i;
[lbl setBackgroundColor:[UIColor orangeColor]];
[lbl setTitle:arrTitles[i] forState:UIControlStateNormal];
[destiView addSubview:lbl];
UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake((CGRectGetMaxX(tempLabel.frame)+margin), height - 5+64, width, 5)];
[self.arrMLineView addObject:lineView];
lineView.hidden = YES;
lineView.backgroundColor = [UIColor redColor];
[destiView addSubview:lineView];
//绑定点击事件
if (i == 1) {
[lbl addTarget:self action:@selector(clickBtnOne:) forControlEvents:UIControlEventTouchUpInside];
}else if (i == 2){
[lbl addTarget:self action:@selector(clickBtnOne:) forControlEvents:UIControlEventTouchUpInside];
}
tempLabel = lbl;
}
}
-(void)testHiddenWithBtn:(UIButton *)sender{
if (_btnTemp != sender) {
[_btnTemp setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_lineViewTemp.hidden = YES;
_btnTemp = sender;
[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
_lineViewTemp = self.arrMLineView[sender.tag];
_lineViewTemp.hidden = NO;
}
}
-(void)clickBtnOne:(UIButton *)sender{
[self testHiddenWithBtn:sender];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- 为
btn
绑定了tag,减少了许多重复代码. btn
不同的点击事件只需要在方法-(void)clickBtnOne:(UIButton *)sender
中判断tag
值即可- 这个方法
-(void)setLablesWithArrOfTitle:(NSArray<NSString *> *)arrTitles andLeftDistance:(CGFloat)lDistance andItWidth:(CGFloat)width andItHeight:(CGFloat)height andYcoordinate:(CGFloat) y andDestiView:(UIView *)destiView
在我的这个博客(http://blog.csdn.net/alex_birdlion/article/details/52932562) 上有介绍,有兴趣的可以看看.