iOS实现简单分栏效果

 更新时间:2022年03月21日 12:01:08   作者:小溪彼岸  
这篇文章主要为大家详细介绍了iOS实现简单分栏效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

效果图:

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

相关文章

  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    这篇文章主要介绍了ios 使用xcode11 新建项目工程 (值得注意的问题),本文分步骤通过图文的形式给大家展示,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • 支持Xcode10和适配iPhone XS Max、iPhone XR的方法

    支持Xcode10和适配iPhone XS Max、iPhone XR的方法

    这篇文章主要介绍了支持Xcode10和适配iPhone XS Max、iPhone XR的方法,文中通过示例代码以及图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-10-10
  • iOS超出父控件范围无法点击问题解决

    iOS超出父控件范围无法点击问题解决

    这篇文章主要介绍了iOS超出父控件范围无法点击问题解决,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • iOS 截取字符串中两个指定字符串中间的字符串方法

    iOS 截取字符串中两个指定字符串中间的字符串方法

    下面小编就为大家分享一篇iOS 截取字符串中两个指定字符串中间的字符串方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • IOS应用内跳转系统设置相关界面的方法

    IOS应用内跳转系统设置相关界面的方法

    在iOS开发中,有时会有跳转系统设置界面的需求,例如提示用户打开蓝牙或者WIFI,提醒用户打开推送或者位置权限等,接下来通过本文给大家介绍IOS应用内跳转系统设置相关界面的方法,喜欢的朋友参考下
    2016-02-02
  • iOS设置圆角的4种方法实例(附性能评测)

    iOS设置圆角的4种方法实例(附性能评测)

    这篇文章主要给大家介绍了关于iOS设置圆角的4种方法,并给大家附上了性能评测,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-01-01
  • IOS 解决UIButton 点击卡顿/延迟的问题

    IOS 解决UIButton 点击卡顿/延迟的问题

    本文主要介绍 IOS UIButton, 这里给大家提供代码实例作为参考,解决 UIButton 点击卡顿或者延迟问题,在开发 IOS 项目的小伙伴如果遇到这样的问题可以参考下
    2016-07-07
  • 超精准的iOS计步器实现代码

    超精准的iOS计步器实现代码

    这篇文章主要为大家详细介绍了超精准的iOS计步器实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • IOS给xcode工程关联pod的实例详解

    IOS给xcode工程关联pod的实例详解

    这篇文章主要介绍了IOS给xcode工程关联pod的实例详解的相关资料,希望大家通过本文能实现这样的需求,需要的朋友可以参考下
    2017-09-09
  • 查看iOS Crash logs的方法

    查看iOS Crash logs的方法

    发布了一个应用,用户使用 的时候crash了,现在想调查为何crash,所以想在这里探讨一下如何查看iphone 手机的crash logs
    2015-06-06

最新评论