iOS开发自定义Segment实现视图切换

释放双眼,带上耳机,听听看~!

1.效果图:

2.CustomSegmentView.h
//
//  CustomSegmentView.h
//  YouOu
//
//  Created by weihong xuan on 2017/2/9.
//  Copyright © 2017年 weihong xuan. All rights reserved.
//
#import <UIKit/UIKit.h>
@protocol CustomSegDelegate <NSObject>
- (void)customSegmentedValueChanged:(UISegmentedControl *)segment;
@end
@interface CustomSegmentView : UIView<UIScrollViewDelegate>
@property (nonatomic, weak) id<CustomSegDelegate> delegate;
@property (nonatomic, strong) UIScrollView * customScr;
@property (nonatomic, strong) NSArray * titleArr;
@property (nonatomic, strong) UIFont * fontSize;
@property (nonatomic, assign) float View_W;
@property (nonatomic, assign) float itmeViewW;
/**
 *
 * background          背景色
 * titleArr            文字Ary
 * fontSize            文字Normal大小(选中时会+2)
 * titleSelectColor    文字Select颜色
 * normalColor         文字Normal颜色
 * index               初始选中
 */
- (void)setSegmentWithBackground:(UIColor *)background titleArray:(NSArray *)titleArr titleFont:(UIFont *)fontSize  titleLineSelectColor:(UIColor *)titleSelectColor normal:(UIColor *)normalColor withselectedIndex:(NSInteger)index;
-(void)setselectedIndex:(NSInteger)index;
//添加右角标
-(void)setRightTopLabelWithAry:(NSArray*)topNumAry;
@end
3.CustomSegmentView.m
//
//  CustomSegmentView.m
//  YouOu
//
//  Created by weihong xuan on 2017/2/9.
//  Copyright © 2017年 weihong xuan. All rights reserved.
//
#import "CustomSegmentView.h"
#define index_width 13.0
#define index_height 13.0
@interface CustomSegmentView ()
@property (nonatomic,strong) UIView * vLineRed;
@property (nonatomic,strong) UISegmentedControl   * segChoose;
@end
@implementation CustomSegmentView
- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame: frame];
    if (self) {
      
    }
    return self;
}
- (void)setSegmentWithBackground:(UIColor *)background titleArray:(NSArray *)titleArr titleFont:(UIFont *)fontSize  titleLineSelectColor:(UIColor *)titleSelectColor normal:(UIColor *)normalColor withselectedIndex:(NSInteger)index{
    for (UIView*tmpV in self.subviews) {
        [tmpV removeFromSuperview];
    }
    BOOL isScr = YES;
    _titleArr = [[NSArray alloc]initWithArray:titleArr];
    _fontSize = fontSize;
    _View_W = self.frame.size.width;
    _itmeViewW = _View_W/titleArr.count;
    if (titleArr.count>1) {
        _customScr = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _customScr.backgroundColor = [UIColor clearColor];
        _customScr.showsHorizontalScrollIndicator = NO;
        _customScr.delegate = self;
        _customScr.contentSize = CGSizeMake(_View_W, self.frame.size.height);
        isScr = YES;
        [self addSubview:_customScr];
    }
    else{
        isScr = NO;
    }
    _segChoose = [[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 0.5, _View_W, self.frame.size.height-1)];
    _segChoose.backgroundColor = background;
    _segChoose.tag = self.tag;
    [_segChoose setTintColor:background];
    _segChoose.layer.borderColor =background.CGColor;
    for (int i = 0; i < titleArr.count; i ++) {
        [_segChoose insertSegmentWithTitle:titleArr[i] atIndex:i animated:NO];
    }
    [_segChoose setTitleTextAttributes:@{NSFontAttributeName:fontSize,NSForegroundColorAttributeName:normalColor} forState:UIControlStateNormal];
    float seleSize = fontSize.pointSize+2.0;
    if (self.frame.size.width<301) {
        seleSize = fontSize.pointSize+1.0;
    }
    [_segChoose setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:seleSize],NSForegroundColorAttributeName:titleSelectColor} forState:UIControlStateSelected];
    [_segChoose addTarget:self action:@selector(segmentedValueChanged:) forControlEvents:UIControlEventValueChanged];
    _segChoose.selectedSegmentIndex = index;
    //选项卡红色的标识条
    
    CGSize t_s = [self getTextWidth:_titleArr[index] withSize:CGSizeMake(_itmeViewW, 20) withFont:_fontSize];
    _vLineRed = [[UIView alloc]initWithFrame:CGRectMake(index * _itmeViewW+(_itmeViewW-t_s.width-20)/2.0 , self.frame.size.height- 2, t_s.width+20, 2)];
    _vLineRed.backgroundColor = titleSelectColor;
    if (isScr==NO) {
        [self addSubview:_segChoose];
        [self addSubview:_vLineRed];
    }
    else{
        [_customScr addSubview:_segChoose];
        [_customScr addSubview:_vLineRed];
    }
}
- (void)segmentedValueChanged:(UISegmentedControl *)seg{
    CGSize t_s = [self getTextWidth:_titleArr[seg.selectedSegmentIndex] withSize:CGSizeMake(_itmeViewW, 20) withFont:_fontSize];
    _vLineRed.frame = CGRectMake(seg.selectedSegmentIndex * _itmeViewW+(_itmeViewW-t_s.width-20)/2.0 , self.frame.size.height- 2, t_s.width+20, 2);
    if ([_delegate respondsToSelector:@selector(customSegmentedValueChanged:)]) {
        [_delegate customSegmentedValueChanged:_segChoose];
    }
}
-(void)setselectedIndex:(NSInteger)index{
    CGSize t_s = [self getTextWidth:_titleArr[index] withSize:CGSizeMake(_itmeViewW, 20) withFont:_fontSize];
    _vLineRed.frame = CGRectMake(index * _itmeViewW+(_itmeViewW-t_s.width-20)/2.0  , self.frame.size.height- 2, t_s.width+20, 2);
    _segChoose.selectedSegmentIndex = index;
}
//添加标记:
-(void)setRightTopLabelWithAry:(NSArray*)topNumAry{
    for (int i=0; i<topNumAry.count; i++) {
        [[_customScr viewWithTag:100+i] removeFromSuperview];
        [[_customScr viewWithTag:200+i] removeFromSuperview];
    }
    for (int i=0; i<topNumAry.count; i++) {
        NSString*titleStr = _titleArr[i];
        CGSize titleSize = [self getTextWidth:titleStr withSize:CGSizeMake(_itmeViewW, 30) withFont:[UIFont systemFontOfSize:15]];
        NSString * text = topNumAry[i];
        
        UIView*superView = [[UIView alloc]initWithFrame:CGRectMake((_itmeViewW+titleSize.width)/2+_itmeViewW*i, 5, 5, 40)];
        superView.backgroundColor = [UIColor clearColor];
        [_customScr addSubview:superView];
        superView.tag = 100+i;
        if ((![text isEqualToString:@"0"])&&(![text isEqualToString:@""])) {
            [self createRightTopIndexLabelWithSize:CGSizeMake(index_width, index_height) tag:200+i borderWidth:1.5 borderColor:[UIColor redColor].CGColor text:text font:[UIFont systemFontOfSize:10] textColor:[UIColor whiteColor] SuperView:superView];
        }
    }
}
//计算字的尺寸:
-(CGSize)getTextWidth:(NSString*)text withSize:(CGSize)size withFont:(UIFont*)font{
    NSDictionary *attrs=@{NSFontAttributeName:font};
    CGSize s=[text boundingRectWithSize:size options:NSStringDrawingTruncatesLastVisibleLine |
              NSStringDrawingUsesLineFragmentOrigin |
              NSStringDrawingUsesFontLeading attributes:attrs context:nil].size;
    return s;
}
//右上角标记
- (UILabel *)createRightTopIndexLabelWithSize:(CGSize)size tag:(NSInteger)tag borderWidth:(CGFloat)borderWidth borderColor:(CGColorRef)borderColor text:(NSString *)text font:(UIFont *)font textColor:(UIColor *)textColor SuperView:(UIView *)superView{
    CGFloat w = size.height/2;
    CGRect superFrame = superView.frame;
    CGFloat x = CGRectGetWidth(superFrame)-10;
    CGFloat y = -2;
    CGFloat height = size.height;
    CGFloat width;
    if (text.length == 1) {
        width = size.width;
    }else if (text.length == 2){
        width = size.width*1.5;
    }else {
        width = size.width*2;
    }
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    label.backgroundColor = [UIColor redColor];
    label.layer.cornerRadius = w;
    label.layer.masksToBounds = YES;
    label.layer.borderWidth = borderWidth;
    label.layer.borderColor = borderColor;
    label.text = text;
    label.textColor = textColor;
    label.font = font;
    label.textAlignment = NSTextAlignmentCenter;
    [superView addSubview:label];
    return label;
}
@end
4.使用:
//
//  ViewController.m
//  testMP4
//
//  Created by weihong xuan on 2017/6/5.
//  Copyright © 2017年 weihong xuan. All rights reserved.
//
#import "ViewController.h"
#import "ZLImageViewDisplayView.h"
#import "CustomSegmentView.h"
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColorFromRGB(0xeeeeee);
    _segmentView = [[CustomSegmentView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 40)];
    _segmentView.delegate = self;
    [_segmentView setSegmentWithBackground:[UIColor whiteColor] titleArray:@[@"金额",@"按距离",@"购买次数"] titleFont:[UIFont systemFontOfSize:14] titleLineSelectColor:[UIColor redColor] normal:[UIColor blackColor] withselectedIndex:0];
    [self.view addSubview:_segmentView];
}
//别忘了在.h文件里添加<CustomSegDelegate>
- (void)customSegmentedValueChanged:(UISegmentedControl *)segment{
    NSLog(@"点击button----%ld",segment.selectedSegmentIndex);
    [_segmentView setRightTopLabelWithAry:@[@"11",@"0",@"10"]];//如果没有可以去掉
}
@end

人已赞赏
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索