網站首頁 編程語言 正文
本文實例為大家分享了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); ? ? ? ? }]; ? ? } }
原文鏈接:https://blog.csdn.net/qiangshuting/article/details/87927375
相關推薦
- 2022-04-26 Swift踩坑實戰之一個字符引發的Crash_Swift
- 2023-03-22 GoLang?string類型深入分析_Golang
- 2024-07-13 Spring AOP 基于注解的方式實現切面遍程
- 2022-07-06 使用pandas兩列轉換成字典的健和值_python
- 2022-12-24 提升Go語言開發效率的小技巧實例(GO語言語法糖)匯總_Golang
- 2022-09-26 C++實現頁面的緩沖區管理器_C 語言
- 2023-01-20 實現.Net7下數據庫定時檢查的方法詳解_實用技巧
- 2022-08-02 c#中task與thread的區別及使用講解_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支