| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- //
- // SectionChooseView.m
- // CommunityService
- //
- // Created by lujh on 2017/3/8.
- // Copyright © 2017年 卢家浩. All rights reserved.
- //
- #define CustomSegmentBtnTag 888
- #define CustomSegmentLineViewTag 275
- #import "SectionChooseView.h"
- @interface SectionChooseView ()
- @property (nonatomic, strong) NSArray * titleArray;
- @end
- @implementation SectionChooseView
- - (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray *)titleArray
- {
- if (self = [super initWithFrame:frame]) {
- _titleArray = titleArray;
- if (_titleArray.count <= 0) return self;
-
- self.backgroundColor = NewNavigationColor;
- _normalBackgroundColor = [UIColor whiteColor];
- _selectBackgroundColor = [UIColor redColor];
- _titleNormalColor = [UIColor lightGrayColor];
- _titleSelectColor = [UIColor blueColor];
- _selectIndex = 0;
- _normalTitleFont = 14.0f;
- _selectTitleFont = 23.0f;
-
- // 初始化UI界面
- [self setUpSubviews];
- }
- return self;
- }
- #pragma mark -初始化UI界面
- - (void)setUpSubviews
- {
- self.clipsToBounds = YES;
- self.layer.borderColor = self.selectBackgroundColor.CGColor;
-
- CGFloat itemWidth = (SCREEN_WIDTH -136-((_titleArray.count-1) * 5)) / _titleArray.count;
- for (int i = 0; i < _titleArray.count; i ++) {
- UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.frame = CGRectMake(i * (itemWidth +5)+68, 29, itemWidth, 27);
- button.clipsToBounds = YES;
- button.tag = CustomSegmentBtnTag + i;
-
- ViewBorderRadius(button, 3, 0.8, NewClearColor);
- [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateNormal font:_normalTitleFont color:_titleNormalColor];
- [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateSelected font:_selectTitleFont color:_titleSelectColor];
-
- [button setBackgroundImage:[self createImageWithColor:_normalBackgroundColor] forState:UIControlStateNormal];
- [button setBackgroundImage:[self createImageWithColor:_normalBackgroundColor] forState:UIControlStateHighlighted];
- [button setBackgroundImage:[self createImageWithColor:_selectBackgroundColor] forState:UIControlStateSelected];
-
- [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
-
- [self addSubview:button];
- [self sendSubviewToBack:button];
-
- if (_selectIndex == i) {
- button.selected = YES;
- button.userInteractionEnabled = NO;
- }
-
- if (i == _titleArray.count - 1) continue;
-
- }
- }
- #pragma mark -分段按钮点击事件
- - (void)buttonClick:(UIButton *)button
- {
- if (button.tag - CustomSegmentBtnTag == _selectIndex) return;
-
- UIButton * oldButton = (UIButton *)[self viewWithTag:_selectIndex + CustomSegmentBtnTag];
- if (oldButton) {
- oldButton.selected = NO;
- oldButton.userInteractionEnabled = YES;
- }
-
- button.selected = YES;
- button.userInteractionEnabled = NO;
-
- _selectIndex = button.tag - CustomSegmentBtnTag;
-
- if (_delegate && [_delegate respondsToSelector:@selector(SectionSelectIndex:)]) {
- [_delegate SectionSelectIndex:_selectIndex];
- }
- }
- - (void)setCornerRadius:(CGFloat)cornerRadius
- {
- _cornerRadius = cornerRadius;
- self.layer.cornerRadius = cornerRadius;
- }
- - (void)setBorderWidth:(CGFloat)borderWidth
- {
- _borderWidth = borderWidth;
- self.layer.borderWidth = borderWidth;
- }
- #pragma mark -设置分段按钮背景颜色
- - (void)setNormalBackgroundColor:(UIColor *)normalBackgroundColor
- {
- if (normalBackgroundColor == _normalBackgroundColor) return;
-
- _normalBackgroundColor = normalBackgroundColor;
- for (int i = 0; i < self.titleArray.count; i ++) {
- UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
- if (button) {
- [button setBackgroundImage:[self createImageWithColor:normalBackgroundColor] forState:UIControlStateNormal];
- [button setBackgroundImage:[self createImageWithColor:normalBackgroundColor] forState:UIControlStateHighlighted];
- }
- }
- }
- - (void)setSelectBackgroundColor:(UIColor *)selectBackgroundColor
- {
- if (selectBackgroundColor == _selectBackgroundColor) return;
-
- _selectBackgroundColor = selectBackgroundColor;
- for (int i = 0; i < self.titleArray.count; i ++) {
- UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
- if (button) {
- [button setBackgroundImage:[self createImageWithColor:selectBackgroundColor] forState:UIControlStateSelected];
- }
-
- UIView * lineView = [self viewWithTag:CustomSegmentLineViewTag + i];
- if (lineView) {
- lineView.backgroundColor = selectBackgroundColor;
- }
- }
-
- self.layer.borderColor = selectBackgroundColor.CGColor;
- }
- #pragma mark -设置字体颜色
- - (void)setTitleNormalColor:(UIColor *)titleNormalColor
- {
- if (titleNormalColor == _titleNormalColor) return;
-
- _titleNormalColor = titleNormalColor;
- for (int i = 0; i < self.titleArray.count; i ++) {
- UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
- if (button) {
-
- [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateNormal font:_normalTitleFont color:titleNormalColor];
- }
- }
- }
- - (void)setTitleSelectColor:(UIColor *)titleSelectColor
- {
- if (titleSelectColor == _titleSelectColor) return;
-
- _titleSelectColor = titleSelectColor;
- for (int i = 0; i < self.titleArray.count; i ++) {
- UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
- if (button) {
-
- [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateSelected font:_selectTitleFont color:titleSelectColor];
- }
- }
- }
- #pragma mark -设置字体大小
- - (void)setNormalTitleFont:(CGFloat)normalTitleFont
- {
- if (normalTitleFont == _normalTitleFont) return;
-
- _normalTitleFont = normalTitleFont;
- for (int i = 0; i < self.titleArray.count; i ++) {
- UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
- if (button) {
-
- [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateNormal font:normalTitleFont color:_titleNormalColor];
- }
- }
- }
- - (void)setSelectTitleFont:(CGFloat)selectTitleFont
- {
- if (selectTitleFont == _selectTitleFont) return;
-
- _selectTitleFont = selectTitleFont;
- for (int i = 0; i < self.titleArray.count; i ++) {
- UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
- if (button) {
-
- [self setBtnTitleNormalOrSelectFont:button buttonTitle:_titleArray[i] state:UIControlStateSelected font:selectTitleFont color:_titleSelectColor];
- }
- }
- }
- - (void)setBtnTitleNormalOrSelectFont:(UIButton *)button buttonTitle:(NSString *)buttonTitle state:(UIControlState)state font:(CGFloat )font color:(UIColor *)color
- {
- NSDictionary * dic = @{
- NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:font],
- NSForegroundColorAttributeName : color
- };
- NSAttributedString * attributedTitle = [[NSAttributedString alloc] initWithString:buttonTitle attributes:dic];
- [button setAttributedTitle:attributedTitle forState:state];
- }
- #pragma mark -选中的item
- - (void)setSelectIndex:(NSInteger)selectIndex
- {
- if (selectIndex >= _titleArray.count || _selectIndex == selectIndex) {
-
- // 首次进入加载第一个界面通知
- [[NSNotificationCenter defaultCenter] postNotificationName:@"ABC" object:nil];
-
- };
-
- _selectIndex = selectIndex;
-
- for (int i = 0; i < _titleArray.count; i ++) {
- UIButton * button = (UIButton *)[self viewWithTag:CustomSegmentBtnTag + i];
- if (button) {
- button.selected = selectIndex == i ? YES : NO;
- button.userInteractionEnabled = selectIndex == i ? NO : YES;
- }
- }
- }
- - (UIImage *)createImageWithColor:(UIColor *)color
- {
- CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
- UIGraphicsBeginImageContext(rect.size);
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSetFillColorWithColor(context, [color CGColor]);
- CGContextFillRect(context, rect);
-
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- return image;
- }
- - (void)dealloc {
-
- [[NSNotificationCenter defaultCenter] removeObserver:self];
-
- }
- @end
|