首頁 > 軟體

iOS實現簡單分欄效果

2022-03-21 16:00:19

本文範例為大家分享了iOS實現簡單分欄效果的具體程式碼,供大家參考,具體內容如下

直接貼程式碼嘍

GMSubfieldViiew.h

#import <UIKit/UIKit.h>

@interface GMSubfieldViiew : UIView

/**
 * select index
 */
@property(nonatomic,copy) void(^clickIndex)(NSInteger index);

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)thiTitles;

/**
 *  預設勾選
 */
@property(nonatomic,assign) NSInteger selectedIndex;

@end

GMSubfieldViiew.m

#import "GMSubfieldViiew.h"

#define lineH 2
@interface GMSubfieldViiew ()
/**
 *  titles
 */
@property(nonatomic,strong) NSArray * titles;
/**
 *  lineView
 */
@property(nonatomic,weak) UIView *lineView;
/**
 *  itemWidth
 */
@property(nonatomic,assign) CGFloat itemWidth;
@end

@implementation GMSubfieldViiew


#pragma mark - initUI
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)thiTitles
{
    if (self = [super initWithFrame:frame]) {
        self.titles = thiTitles;
        //initSubViews
        [self initSubViews];
    }
    return self;
}

#pragma mark - action
- (void) initSubViews
{
    self.itemWidth = kScreen_Width/self.titles.count;
    //add child
    for (int i=0; i<self.titles.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:self.titles[i] forState:UIControlStateNormal];
        btn.titleLabel.font = FontSize(15);
        btn.tag  = 100+i;
        btn.layer.borderWidth = 0.5;
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
    }

    //新增下劃線
    UIView *lineView  = [[UIView alloc]init];
    lineView.backgroundColor = [UIColor blackColor];
    [self addSubview:lineView];
    self.lineView     = lineView;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    for (int i=0; i<self.titles.count; i++) {
        UIButton *btn = [self viewWithTag:100+i];
        btn.frame = CGRectMake(i*self.itemWidth, 0, self.itemWidth, self.bounds.size.height-lineH+1);
    }
    self.lineView.frame = CGRectMake(self.selectedIndex*self.itemWidth, self.bounds.size.height-lineH, self.itemWidth, lineH);
}


- (void) btnClick:(UIButton *)btn
{
    NSInteger index = btn.tag -100;
    ESWeakSelf
    [UIView animateWithDuration:0.2 animations:^{
        ESStrongSelf
        self.lineView.frame = CGRectMake(index*self.itemWidth, self.bounds.size.height-lineH, self.itemWidth, lineH);
    }];
    if (self.clickIndex) {
        self.clickIndex(index);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

呼叫:

GMSubfieldViiew *segView = [[GMSubfieldViiew alloc]initWithFrame:CGRectMake(0, 10, kScreen_Width, segH) titles:@[@"未還",@"已還"]];
    segView.selectedIndex = 1;
    ESWeakSelf
    segView.clickIndex = ^(NSInteger index){
        self.isHK = NO;
        ESStrongSelf
        if(index==0){
            //未還
            self.rightButton.hidden = NO;
        }
        else if(index==1){
           //已還
            self.rightButton.hidden = YES;
            self.containView.hidden = YES;
        }
        self.tableView.frame = CGRectMake(0, 60, kScreen_Width, kScreen_Height-NavHeight-60);
        [self.tableView reloadData];
    };
   [self.view addSubView:segView];

效果圖:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com