| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- //
- // ModelTitleView.m
- // model
- //
- // Created by JuYi on 2018/7/17.
- // Copyright © 2018年 Mine. All rights reserved.
- // 选择按钮
- #import "ModelTitleView.h"
- @interface ModelTitleView()<UIScrollViewDelegate>
- @property (nonatomic, strong) NSMutableArray *titleBettons;
- @property (nonatomic, weak) UIScrollView *titleScrollView;
- @property (nonatomic, weak) UIButton *selectBtn;
- @property (nonatomic, strong) UIView *lineView; //下划线
- @end
- @implementation ModelTitleView
- - (NSMutableArray *)titleBettons {
- if (!_titleBettons) {
- _titleBettons = [NSMutableArray arrayWithCapacity:0];
- }
- return _titleBettons;
- }
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundColor = [UIColor whiteColor];
-
- }
- return self;
- }
- - (void)setTitleArr:(NSArray *)titleArr {
- _titleArr = titleArr;
- [self setUpAllChildTitle];
- }
- /**
- 添加字标题
- */
- - (void)setUpAllChildTitle {
- CGFloat btnh = self.bounds.size.height;
- //
- UIScrollView *titleScrollView = [[UIScrollView alloc] init];
- titleScrollView.frame = CGRectMake(0, 0, ScreenWidth,btnh);
- // titleScrollView.delegate = self;
- [self addSubview:titleScrollView];
- self.titleScrollView = titleScrollView;
- //
- NSInteger count = self.titleArr.count;
- CGFloat btnx = 0;
- CGFloat btny = 0;
- CGFloat btnw = ScreenWidth / count;
-
-
- for (NSInteger i = 0; i < count; i++) {
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
- [btn setTitle:self.titleArr[i] forState:UIControlStateNormal];
- btnx = btnw * i;
- btn.frame = CGRectMake(btnx, btny, btnw, btnh);
- btn.tag = i;
- [btn setTitleColor:RGBValueColor(0x333333, 1.0) forState:UIControlStateNormal];
- btn.titleLabel.font = [UIFont systemFontOfSize:15];
- [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
- if (i == 0) {
- [self btnClick:btn];
- }
- [self.titleBettons addObject:btn];
- [self.titleScrollView addSubview:btn];
-
- }
- // CGFloat contentWidth = ScreenWidth < 375 ? 375 : ScreenWidth;
- // self.titleScrollView.contentSize = CGSizeMake(contentWidth, 0);
- // self.titleScrollView.showsHorizontalScrollIndicator = NO;
-
- self.lineView = [[UIView alloc] init];
- self.lineView.frame = CGRectMake(0, 0, 35, 2);
- self.lineView.backgroundColor = RGBValueColor(0xfe3f87, 1.0);
- UIButton *button = self.titleBettons[0];
- self.lineView.center = CGPointMake(button.center.x, btnh - 2);
- [self.titleScrollView addSubview:self.lineView];
- //
- self.longLineView = [[UIImageView alloc] initWithFrame:CGRectMake(0, btnh - 1, ScreenWidth, 1)];
- self.longLineView.image = [UIImage imageNamed:@"fengexian"];
- [self.titleScrollView addSubview:self.longLineView];
-
- }
- - (void)btnClick:(UIButton *)sender {
- [_selectBtn setTitleColor:RGBValueColor(0x333333, 1.0) forState:UIControlStateNormal];
- [sender setTitleColor:RGBValueColor(0xfe3f87, 1.0) forState:UIControlStateNormal];
- self.lineView.center = CGPointMake(sender.center.x, self.frame.size.height - 2);
- _selectBtn = sender;
- if (self.ModelTitleViewBlock) {
- self.ModelTitleViewBlock(sender.tag);
- }
- }
- @end
|