iOS UISegmentControl实现自定义分栏效果

2022-10-07,,,,

本文实例为大家分享了ios uisegmentcontrol实现自定义分栏效果的具体代码,供大家参考,具体内容如下

ios 自带的uisegmentcontrol实现的就是类似上图的效果
但是很多用处 一般这两个分栏是两个tableview,需要左右滑动来响应分栏

下面来讲述这样的效果是怎么实现的呢?

主要那里有一根短红线,需要滑动 来切换tableview

先自定义一个segmentview

.h

//定义block,用来传递点击的第几个按钮
typedef void (^passvalueblock)(nsinteger index);

@interface bclcommunitysegmentview : uiview

//定义一下block
@property (nonatomic, strong) passvalueblock returnblock;
@property (nonatomic, strong) uiimageview *selectimage;//这个就是短红线

//初始化数组,传入frame和名称
- (id) initwithframe:(cgrect)frame withtitlearray:(nsarray *)array;

//block传递值方法
- (void)setreturnblock:(passvalueblock)returnblock;
@end

在segmentview.m中
循环创建按钮,几个分栏创建几个按钮

- (void)creatsegmentview {
    //设置按钮的宽度
    _itemwidth = self.frame.size.width / _itemcounts;
    //循环创建按钮
    for (int i = 0; i < _itemcounts; i++) {
        uibutton *button  = [[uibutton alloc]initwithframe:cgrectmake((i + 1) *_itemwidth/2, 0, _itemwidth/2, self.frame.size.height)];
        [self addsubview:button];
        
        //设置button的字
        [button settitle:_titlearray[i] forstate:uicontrolstatenormal];
        //设置button的字颜色
        
        [button settitlecolor:[uicolor blackcolor] forstate:uicontrolstatenormal];
        //设置字体大小
        button.titlelabel.font = [uifont systemfontofsize:20];
        //设置居中显示
        button.titlelabel.textalignment = nstextalignmentcenter;
        //设置tag值
        button.tag = 1000 + i;
        //添加点击事件
        [button addtarget:self action:@selector(buttonaction:) forcontrolevents:uicontroleventtouchupinside];
        //如果是第一个,默认被选中
        if (i == 0) {
            button.selected = yes;
        }
    }
    
    
    //添加一个select
    _selectimage = [[uiimageview alloc]initwithframe:cgrectmake(_itemwidth / 2, self.frame.size.height - 2, _itemwidth / 2, 2)];
    _selectimage.image = [uiimage imagenamed:@"bcl_bg_community_segment_color_line"];
    [self addsubview:_selectimage];
}

然后设置按钮的点击事件,将点击到哪个按钮 回调过去

-(void)buttonaction:(uibutton *)button{
    
    //当button被点击,所有的button都设为未选中状态
    for (uiview *view in self.subviews) {
        if ([view iskindofclass:[uibutton class]]) {
            uibutton *subbutton = (uibutton*)view;
            subbutton.selected = no;
            subbutton.titlelabel.font = [uifont systemfontofsize:20];
        }
    }
    //然后将选中的这个button变为选中状态
    button.selected = yes;
    
    //通过当前的tag值设置select的位置
    nsinteger index = button.tag - 1000;
    [uiview animatewithduration:0.3 animations:^{
        self->_selectimage.frame = cgrectmake((1 + index)*_itemwidth/2, _selectimage.frame.origin.y, self->_selectimage.frame.size.width, _selectimage.frame.size.height);
    }];
    
    _returnblock(index);
}

在需要展现的controller中

.h

@interface bclcommunityview : uiview

@property (nonatomic, strong) uiscrollview *scrollerview;
@property(nonatomic ,strong) uitableview *circletableview;
@property(nonatomic ,strong) uitableview *squretableview;
@property (nonatomic, strong)bclcommunitysegmentview *segmentview;

@end

在.m中用scrollview实现分栏的两个tableview的滑动

- (instancetype) initwithframe:(cgrect)frame {
    if(self = [super initwithframe:frame]) {
        [self setsegmentview];
        
        _circletableview = [self loadtableview];
        _squretableview = [self loadtableview];
        
        _circletableview.tag = 1;
        _squretableview.tag = 2;
        
        _scrollerview = [[uiscrollview alloc] init];
        _scrollerview.frame = cgrectmake(0, 104, kscreenw, kscreenh);
        _scrollerview.pagingenabled = yes;
        _scrollerview.scrollenabled = yes;
        _scrollerview.contentsize = cgsizemake(kscreenw * 2, kscreenh);
        _scrollerview.bounces = yes;
        _scrollerview.delegate = self;
        
        [_scrollerview addsubview:_circletableview];
        [_scrollerview addsubview:_squretableview];
        
        _circletableview.frame = cgrectmake(0, 0, kscreenw, kscreenh);
        _squretableview.frame = cgrectmake(kscreenw, 0, kscreenw, kscreenh);
        [self addsubview:_scrollerview];
    }
    return self;
}
- (uitableview *)loadtableview
{
    uitableview  *tableview = [[uitableview alloc] initwithframe:cgrectmake(0, 0, kscreenw, kscreenh) style:uitableviewstylegrouped];
    tableview.showsverticalscrollindicator = no;
    [tableview registerclass:[uitableviewcell class] forcellreuseidentifier:@"cell"];
    
    tableview.datasource = self;
    
    [self addsubview:tableview];
    return tableview;
}
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview {
    if(tableview.tag == 1) {
        return 3;
    } else {
         return 2;
    }
   
}
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {
    return 1;
}
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {
    if(tableview.tag == 1) {
        uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"cell" forindexpath:indexpath];
        if(!cell) {
            cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:@"cell"];
            
        }
        cell.backgroundcolor = [uicolor redcolor];
        
        cell.textlabel.text = @"11111";
        return cell;
    } else {
        uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"cell" forindexpath:indexpath];
        if(!cell) {
            cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:@"cell"];
        }
        return cell;
    }
    
}

scrollview代理 滑动scrollerview实现小红条的滑动

- (void)scrollviewdidenddecelerating:(uiscrollview *)scrollview {
    cgrect frame = _segmentview.selectimage.frame;
    if(scrollview.contentoffset.x / kscreenw == 0) {
        [uiview animatewithduration:0.1 animations:^{
        _segmentview.selectimage.frame = cgrectmake(kscreenw / 4, frame.origin.y, frame.size.width, frame.size.height);
        }];
    } else if(scrollview.contentoffset.x / kscreenw == 1){
        [uiview animatewithduration:0.1 animations:^{
            _segmentview.selectimage.frame = cgrectmake(kscreenw / 2, frame.origin.y, frame.size.width, frame.size.height);
        }];
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

《iOS UISegmentControl实现自定义分栏效果.doc》

下载本文的Word格式文档,以方便收藏与打印。