網站首頁 編程語言 正文
本文實例為大家分享了iOS Segment帶滑動條切換效果的具體代碼,供大家參考,具體內容如下
#import "ViewController.h" ? @interface ViewController () ? @property (nonatomic,strong) NSArray *arrTitle; ? @property (nonatomic,strong) UIView *flyBar; ? @end ? @implementation ViewController ? - (void)viewDidLoad { ? ? [super viewDidLoad]; ? ? // Do any additional setup after loading the view, typically from a nib. ? ?? ? ? _arrTitle = [[NSArray alloc] initWithObjects:@"標題1",@"標題2",@"標題3",@"標題4", nil]; ? ?? ? ? UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)]; ? ? baseView.backgroundColor = [UIColor orangeColor]; ? ? [self.view addSubview:baseView]; ? ?? ? ? for (int i=0; i<_arrTitle.count; i++) { ? ? ? ? UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/_arrTitle.count*i, 20,self.view.frame.size.width/_arrTitle.count, 40)]; ? ? ? ? [btn setTitle:[_arrTitle objectAtIndex:i] forState:UIControlStateNormal]; ? ? ? ? [btn setTag:100+i]; ? ? ? ? [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; ? ? ? ? [baseView addSubview:btn]; ? ? } ? ?? ? ? _flyBar = [[UIView alloc] initWithFrame:CGRectMake(0, baseView.frame.size.height-2, self.view.frame.size.width/_arrTitle.count, 2)]; ? ? _flyBar.backgroundColor = [UIColor redColor]; ? ? [baseView addSubview:_flyBar]; } ? - (void)btnClick:(id)sender { ? ? NSInteger tagNum = [sender tag]; ? ? [self updateButtonClickState:tagNum]; } ? //更新按鈕點擊效果 - (void)updateButtonClickState:(NSInteger)tagNum { ? ? UIButton *currentBtn = (UIButton *)[self.view viewWithTag:tagNum]; ? ?? ? ? for (int i=100; i<_arrTitle.count+100; i++) { ? ? ? ? if (i != tagNum) { ? ? ? ? ? ? UIButton *btn = (UIButton *)[self.view viewWithTag:i]; ? ? ? ? ? ? [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; ? ? ? ? } ? ? } ? ?? ? ? [UIView animateKeyframesWithDuration:0.1 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?delay:0.0 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?options:UIViewKeyframeAnimationOptionLayoutSubviews ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? animations:^{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? _flyBar.center = CGPointMake(currentBtn.center.x, _flyBar.center.y); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? completion:^(BOOL finished) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [currentBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }]; } ? ? @end
原文鏈接:https://blog.csdn.net/u010545480/article/details/47428535
相關推薦
- 2022-10-26 python?NumPy讀取和保存點云數據實現_python
- 2022-08-02 python生產環境禁用assert斷言的方法_python
- 2022-08-14 淺談Python任務自動化工具Tox基本用法_python
- 2022-07-24 .Net創建型設計模式之建造者、生成器模式(Builder)_基礎應用
- 2022-05-23 iOS?Segment帶滑動條切換效果_IOS
- 2022-03-24 一篇文章教你如何用C語言實現strcpy和strlen_C 語言
- 2023-12-09 SpringBoot自定義異常處理機制
- 2022-12-13 Python使用自定義裝飾器的示例詳解_python
- 最近更新
-
- 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同步修改后的遠程分支