網(wǎng)站首頁 編程語言 正文
iOS?UISegmentControl實現(xiàn)自定義分欄效果_IOS
作者:lalu??于?2019-02-25?23:49:34?發(fā)布??3851 ? 更新時間: 2022-05-23 編程語言本文實例為大家分享了iOS UISegmentControl實現(xiàn)自定義分欄效果的具體代碼,供大家參考,具體內(nèi)容如下
iOS 自帶的UISegmentControl實現(xiàn)的就是類似上圖的效果
但是很多用處 一般這兩個分欄是兩個tableView,需要左右滑動來響應分欄
下面來講述這樣的效果是怎么實現(xiàn)的呢?
主要那里有一根短紅線,需要滑動 來切換tableView
先自定義一個SegmentView
.h
//定義block,用來傳遞點擊的第幾個按鈕 typedef void (^PassValueBlock)(NSInteger index); @interface BCLCommunitySegmentView : UIView //定義一下block @property (nonatomic, strong) PassValueBlock returnBlock; @property (nonatomic, strong) UIImageView *selectImage;//這個就是短紅線 //初始化數(shù)組,傳入frame和名稱 - (id) initWithFrame:(CGRect)frame withTitleArray:(NSArray *)array; //block傳遞值方法 - (void)setReturnBlock:(PassValueBlock)returnBlock; @end
在SegmentView.m中
循環(huán)創(chuàng)建按鈕,幾個分欄創(chuàng)建幾個按鈕
- (void)creatSegmentView { ? ? //設置按鈕的寬度 ? ? _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]; ? ? ? ?? ? ? ? ? //設置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]; }
然后設置按鈕的點擊事件,將點擊到哪個按鈕 回調(diào)過去
-(void)buttonAction:(UIButton *)button{ ? ?? ? ? //當button被點擊,所有的button都設為未選中狀態(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]; ? ? ? ? } ? ? } ? ? //然后將選中的這個button變?yōu)檫x中狀態(tài) ? ? 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); }
在需要展現(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實現(xiàn)分欄的兩個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實現(xiàn)小紅條的滑動
- (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); ? ? ? ? }]; ? ? } }
原文鏈接:https://blog.csdn.net/qiangshuting/article/details/87927375
相關(guān)推薦
- 2022-05-13 CLion 中文輸出亂碼
- 2022-02-11 idea package合在一起,利用Compact Middle Packages解決 &
- 2022-08-14 Python基礎教程之pip的安裝和卸載_python
- 2022-06-11 C#實現(xiàn)文件Move和Copy操作_C#教程
- 2022-04-22 nvm切換node版本后提示npm : 無法將“npm”項識別為 cmdlet、函數(shù)、腳本文件或可運
- 2023-02-17 Python排序算法之堆排序算法_python
- 2022-04-15 使用PyInstaller?打包配置文件_python
- 2022-05-04 C#設計模式之策略模式_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支