溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

iOS?UISegmentControl如何實(shí)現(xiàn)自定義分欄效果

發(fā)布時(shí)間:2022-03-21 13:33:37 來(lái)源:億速云 閱讀:404 作者:小新 欄目:開(kāi)發(fā)技術(shù)

小編給大家分享一下iOS UISegmentControl如何實(shí)現(xiàn)自定義分欄效果,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

具體內(nèi)容如下

iOS?UISegmentControl如何實(shí)現(xiàn)自定義分欄效果

iOS 自帶的UISegmentControl實(shí)現(xiàn)的就是類似上圖的效果
但是很多用處 一般這兩個(gè)分欄是兩個(gè)tableView,需要左右滑動(dòng)來(lái)響應(yīng)分欄

下面來(lái)講述這樣的效果是怎么實(shí)現(xiàn)的呢?

iOS?UISegmentControl如何實(shí)現(xiàn)自定義分欄效果

主要那里有一根短紅線,需要滑動(dòng) 來(lái)切換tableView

先自定義一個(gè)SegmentView

.h

//定義block,用來(lái)傳遞點(diǎn)擊的第幾個(gè)按鈕
typedef void (^PassValueBlock)(NSInteger index);

@interface BCLCommunitySegmentView : UIView

//定義一下block
@property (nonatomic, strong) PassValueBlock returnBlock;
@property (nonatomic, strong) UIImageView *selectImage;//這個(gè)就是短紅線

//初始化數(shù)組,傳入frame和名稱
- (id) initWithFrame:(CGRect)frame withTitleArray:(NSArray *)array;

//block傳遞值方法
- (void)setReturnBlock:(PassValueBlock)returnBlock;
@end

在SegmentView.m中
循環(huán)創(chuàng)建按鈕,幾個(gè)分欄創(chuàng)建幾個(gè)按鈕

- (void)creatSegmentView {
    //設(shè)置按鈕的寬度
    _itemWidth = self.frame.size.width / _itemCounts;
    //循環(huán)創(chuàng)建按鈕
    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];
        
        //設(shè)置button的字
        [button setTitle:_titleArray[i] forState:UIControlStateNormal];
        //設(shè)置button的字顏色
        
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //設(shè)置字體大小
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        //設(shè)置居中顯示
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        //設(shè)置tag值
        button.tag = 1000 + i;
        //添加點(diǎn)擊事件
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        //如果是第一個(gè),默認(rèn)被選中
        if (i == 0) {
            button.selected = YES;
        }
    }
    
    
    //添加一個(gè)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];
}

然后設(shè)置按鈕的點(diǎn)擊事件,將點(diǎn)擊到哪個(gè)按鈕 回調(diào)過(guò)去

-(void)buttonAction:(UIButton *)button{
    
    //當(dāng)button被點(diǎn)擊,所有的button都設(shè)為未選中狀態(tài)
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton *subButton = (UIButton*)view;
            subButton.selected = NO;
            subButton.titleLabel.font = [UIFont systemFontOfSize:20];
        }
    }
    //然后將選中的這個(gè)button變?yōu)檫x中狀態(tài)
    button.selected = YES;
    
    //通過(guò)當(dāng)前的tag值設(shè)置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);
}

在需要展現(xiàn)的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實(shí)現(xiàn)分欄的兩個(gè)tableView的滑動(dòng)

- (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代理 滑動(dòng)scrollerView實(shí)現(xiàn)小紅條的滑動(dòng)

- (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如何實(shí)現(xiàn)自定義分欄效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI