CBSegmentView

#import

typedef void(^titleChooseBlock)(NSInteger x);

typedefNS_ENUM(NSInteger,CBSegmentStyle) {

    /**

     * By default, there is a slider on the bottom.

     */

    CBSegmentStyleSlider =0,

    /**

     * This flag will zoom the selected text label.

     */

    CBSegmentStyleZoom  =1,

};

@interface CBSegmentView : UIScrollView

@property (nonatomic, copy) titleChooseBlock titleChooseReturn;

/**

 * Set segment titles and titleColor.

 *

 * @paramtitleArrayThe titles segment will show.

 */

- (void)setTitleArray:(NSArray *)titleArray;

/**

 * Set segment titles and titleColor.

 *

 * @paramtitleArrayThe titles segment will show.

 * @paramstyleThe segment style.

 */

- (void)setTitleArray:(NSArray *)titleArraywithStyle:(CBSegmentStyle)style;

/**

 * Set segment titles and titleColor.

 *

 * @paramtitleArrayThe titles segment will show.

 * @paramtitleColorThe normal title color.

 * @paramselectedColorThe selected title color.

 * @paramstyleThe segment style.

 */

- (void)setTitleArray:(NSArray *)titleArray

            titleFont:(CGFloat)font

           titleColor:(UIColor*)titleColor

   titleSelectedColor:(UIColor*)selectedColor

            withStyle:(CBSegmentStyle)style;

@end

@interfaceUIView(CBViewFrame)

@property (nonatomic, assign) CGFloat cb_Width;

@property (nonatomic, assign) CGFloat cb_Height;

@property (nonatomic, assign) CGFloat cb_CenterX;

@property (nonatomic, assign) CGFloat cb_CenterY;

@end






#import "CBSegmentView.h"

@interface CBSegmentView ()

/**

 *  configuration.

 */

{

    CGFloat_HeaderH;

    UIColor*_titleColor;

    UIColor*_titleSelectedColor;

    CBSegmentStyle_SegmentStyle;

    CGFloat_titleFont;

}

/**

 *  The bottom red slider.

 */

@property (nonatomic, weak) UIView *slider;

@property (nonatomic, strong) NSMutableArray *titleWidthArray;

@property (nonatomic, weak) UIButton *selectedBtn;

@end

#define CBColorA(r,g,b,a) [UIColor colorWithRed:(r)/255.0green:(g)/255.0blue:(b)/255.0alpha:a]

#define CBScreenH [UIScreen mainScreen].bounds.size.height

#define CBScreenW [UIScreen mainScreen].bounds.size.width

@implementation CBSegmentView

#pragma mark - delayLoading

- (NSMutableArray *)titleWidthArray {

    if (!_titleWidthArray) {

        _titleWidthArray = [NSMutableArray new];

    }

    return _titleWidthArray;

}

#pragma mark - Initialization

- (instancetype)initWithFrame:(CGRect)frame

{

    self= [superinitWithFrame:frame];

    if(self) {

        self.bounces=NO;

        self.showsHorizontalScrollIndicator = NO;

        self.showsVerticalScrollIndicator = NO;

        self.layer.borderColor=CBColorA(204,204,204,1).CGColor;

        self.layer.borderWidth = 0.5;


        _HeaderH= frame.size.height;

        _SegmentStyle = CBSegmentStyleSlider;

        _titleColor = [UIColor darkTextColor];

        _titleSelectedColor=CBColorA(199,13,23,1);

        _titleFont=15;

    }

    return self;

}

- (void)setTitleArray:(NSArray *)titleArray {

    [self setTitleArray:titleArray withStyle:0];

}

- (void)setTitleArray:(NSArray *)titleArraywithStyle:(CBSegmentStyle)style {

    [self setTitleArray:titleArray titleFont:0 titleColor:nil titleSelectedColor:nil withStyle:style];

}

- (void)setTitleArray:(NSArray *)titleArray

            titleFont:(CGFloat)font

           titleColor:(UIColor*)titleColor

   titleSelectedColor:(UIColor*)selectedColor

            withStyle:(CBSegmentStyle)style {


//    set style

    if(style !=0) {

        _SegmentStyle= style;

    }

    if(font !=0) {

        _titleFont= font;

    }

    if(titleColor) {

        _titleColor= titleColor;

    }

    if(selectedColor) {

        _titleSelectedColor= selectedColor;

    }


    if (style == CBSegmentStyleSlider) {

        UIView*slider = [[UIViewalloc]init];

        slider.frame=CGRectMake(0,_HeaderH-2,0,2);

        slider.backgroundColor=_titleSelectedColor;

        [selfaddSubview:slider];

        self.slider= slider;

    }


    [self.titleWidthArray removeAllObjects];

    CGFloattotalWidth =15;

    CGFloatbtnSpace =15;

    for(NSIntegeri =0; i

//        cache title width

        CGFloattitleWidth = [selfwidthOfTitle:titleArray[i]titleFont:_titleFont];

        [self.titleWidthArray addObject:[NSNumber numberWithFloat:titleWidth]];

//        creat button

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        [selfaddSubview:btn];

        CGFloatbtnW = titleWidth+20;

        btn.frame=  CGRectMake(totalWidth,0.5, btnW,_HeaderH-0.5-2);

        btn.contentMode = UIViewContentModeCenter;

        btn.titleLabel.textAlignment = NSTextAlignmentCenter;

        btn.tag= i;

        [btnsetTitle:titleArray[i] forState:UIControlStateNormal];

        [btnsetTitleColor:_titleColor forState:UIControlStateNormal];

        [btnsetTitleColor:_titleSelectedColor forState:UIControlStateSelected];

        [btn.titleLabel setFont:[UIFont systemFontOfSize:_titleFont]];

        [btnaddTarget:self action:@selector(titleButtonSelected:) forControlEvents:UIControlEventTouchUpInside];

        totalWidth = totalWidth+btnW+btnSpace;

        if(i ==0) {

            btn.selected=YES;

            self.selectedBtn= btn;

            if (_SegmentStyle == CBSegmentStyleSlider) {

                self.slider.cb_Width= titleWidth;

                self.slider.cb_CenterX= btn.cb_CenterX;

            }else if (_SegmentStyle == CBSegmentStyleZoom) {

                self.selectedBtn.transform = CGAffineTransformMakeScale(1.3, 1.3);

            }

        }

    }

    totalWidth = totalWidth+btnSpace;

    self.contentSize=CGSizeMake(totalWidth,0);

}

//  button click

- (void)titleButtonSelected:(UIButton *)btn {

    self.selectedBtn.selected = NO;

    btn.selected=YES;

    if (_SegmentStyle == CBSegmentStyleSlider) {

        NSNumber* sliderWidth =self.titleWidthArray[btn.tag];

        [UIView animateWithDuration:0.2 animations:^{

            self.slider.cb_Width= sliderWidth.floatValue;

            self.slider.cb_CenterX = btn.cb_CenterX;

        }];

    }else if (_SegmentStyle == CBSegmentStyleZoom) {

        [UIView animateWithDuration:0.2 animations:^{

            self.selectedBtn.transform = CGAffineTransformIdentity;

            btn.transform = CGAffineTransformMakeScale(1.3, 1.3);


        }];

    }

    self.selectedBtn= btn;

    CGFloatoffsetX = btn.cb_CenterX-self.frame.size.width*0.5;

    if(offsetX<0) {

        offsetX =0;

    }

    if (offsetX>self.contentSize.width-self.frame.size.width) {

        offsetX =self.contentSize.width-self.frame.size.width;

    }

    [self setContentOffset:CGPointMake(offsetX, 0) animated:YES];

    if (self.titleChooseReturn) {

        self.titleChooseReturn(btn.tag);

    }

}

//  cache title width

- (CGFloat)widthOfTitle:(NSString*)titletitleFont:(CGFloat)titleFont {

    CGSize titleSize = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, _HeaderH-2)

                                            options:NSStringDrawingUsesLineFragmentOrigin

                                         attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:titleFont] forKey:NSFontAttributeName]

                                            context:nil].size;

    returntitleSize.width;

}

@end

@implementationUIView(CBViewFrame)

- (void)setCb_Width:(CGFloat)cb_Width {

    CGRectframe =self.frame;

    frame.size.width= cb_Width;

    self.frame= frame;

}

- (CGFloat)cb_Width {

    return self.frame.size.width;

}

- (void)setCb_Height:(CGFloat)cb_Height {

    CGRectframe =self.frame;

    frame.size.height= cb_Height;

    self.frame= frame;

}

- (CGFloat)cb_Height {

    return self.frame.size.height;

}

- (void)setCb_CenterX:(CGFloat)cb_CenterX {

    CGPointcenter =self.center;

    center.x= cb_CenterX;

    self.center= center;

}

- (CGFloat)cb_CenterX {

    return self.center.x;

}

- (void)setCb_CenterY:(CGFloat)cb_CenterY {

    CGPointcenter =self.center;

    center.y= cb_CenterY;

    self.center= center;

}

- (CGFloat)cb_CenterY {

    return self.center.y;

}

@end

你可能感兴趣的:(CBSegmentView)