这篇文章主要为大家详细介绍了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.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



以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
织梦狗教程
本文标题为:iOS Segment带滑动条切换效果
基础教程推荐
猜你喜欢
- IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
- Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
- IOS应用内跳转系统设置相关界面的方法 2022-11-20
- iOS开发教程之XLForm的基本使用方法 2023-05-01
- android studio按钮监听的5种方法实例详解 2023-01-12
- Android多返回栈技术 2023-04-15
- Flutter绘图组件之CustomPaint使用详解 2023-05-12
- Flutter手势密码的实现示例(附demo) 2023-04-11
- Android中的webview监听每次URL变化实例 2023-01-23
- 解决Android Studio突然不显示logcat日志的问题 2023-02-04
