欢迎来到代码驿站!

当前位置:首页 >

iOS实现简单分栏效果

时间:2023-03-01 15:11:17|栏目:|点击:

本文实例为大家分享了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];

效果图:

上一篇:python环境功能强大的pip-audit安全漏洞扫描工具

栏    目:

下一篇:android Retrofit2+okHttp3使用总结

本文标题:iOS实现简单分栏效果

本文地址:http://www.codeinn.net/misctech/226683.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有