iOS|iOS Segment带滑动条切换效果
本文实例为大家分享了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.1delay:0.0options:UIViewKeyframeAnimationOptionLayoutSubviewsanimations:^{_flyBar.center = CGPointMake(currentBtn.center.x, _flyBar.center.y); }completion:^(BOOL finished) {[currentBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; }]; } @end
文章图片
文章图片
文章图片
文章图片
【iOS|iOS Segment带滑动条切换效果】以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- 学生视角手把手带你写Java|学生视角手把手带你写Java 线程池上
- 学生视角手把手带你写Java|学生视角手把手带你写Java 线程池改良版
- 吃透前端工程化,大厂级实战项目以战带练
- 数据结构|3000字带你深入理解二叉树(图解剖析)
- 一些JDK自带的性能分析利器
- 微带线贴片天线尺寸的计算,利用Matlab计算+常见的RF计算在线公式
- TypeScript-axios模块进行封装的操作与一些想法
- Axios|Axios 教程(Vue + Axios 安装及实战 - 手把手教你搭建加密币实时价格看板)
- Pygame合集|【普通玩家VS高手玩家】一带传奇游戏《俄罗斯方块儿》新作—实现AI自动玩游戏~
- springsecurity|SpringBoot整合SpringSecurity带图片验证码简单实现