iOS UISegmentControl实现自定义分栏效果

 更新时间:2022年03月21日 11:41:46   作者:lalu  于 2019-02-25 23:49:34 发布  3851  
这篇文章主要为大家详细介绍了iOS UISegmentControl实现自定义分栏效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

本文实例为大家分享了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开发底层探索界面优化示例详解

    iOS开发底层探索界面优化示例详解

    这篇文章主要为大家介绍了iOS开发底层探索界面优化示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • iOS实现拼图小游戏

    iOS实现拼图小游戏

    这篇文章主要为大家详细介绍了iOS实现拼图小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • iOS下拉选择菜单简单封装

    iOS下拉选择菜单简单封装

    这篇文章主要为大家详细介绍了iOS下拉选择菜单封装代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • iOS自带原生二维码扫描的实现

    iOS自带原生二维码扫描的实现

    最近项目中需要做一个二维码扫描,虽然有很多二维码扫描的第三方可以用,但是考虑到项目中的需要,所以我放弃了使用三方库,而采用了苹果原生的扫描。下面这篇文章就介绍了iOS自带原生二维码扫描的实现,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-01-01
  • iOS实现实时检测网络状态的示例代码

    iOS实现实时检测网络状态的示例代码

    网络连接状态检测对于我们的iOS开发来说是一个非常通用的需求。下面这篇文章主要就给大家介绍了关于利用iOS实现实时检测网络状态的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-07-07
  • iOS开发实现简单抽屉效果

    iOS开发实现简单抽屉效果

    这篇文章主要为大家详细介绍了iOS开发实现简单抽屉效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • iOS微信分享后关闭发送成功提示并返回应用

    iOS微信分享后关闭发送成功提示并返回应用

    这篇文章主要为大家详细介绍了iOS微信分享后关闭发送成功提示并返回应用的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • iOS本地动态生成验证码的方法

    iOS本地动态生成验证码的方法

    这篇文章主要介绍了iOS本地动态生成验证码的方法,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-01-01
  • IOS中UITextView或UITextField字数限制的实现

    IOS中UITextView或UITextField字数限制的实现

    这篇文章主要介绍了IOS中UITextView或UITextField字数限制的实现的相关资料,希望通过本文能帮助到大家实现这样的功能,需要的朋友可以参考下
    2017-10-10
  • iOS中状态栏的基本使用方法汇总

    iOS中状态栏的基本使用方法汇总

    在iOS开发过程中,经常会设置状态栏的样式,所以下面这篇文章主要给大家介绍了关于iOS中状态栏的基本使用的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-07-07

最新评论

?


http://www.vxiaotou.com