本文实例为大家分享了iOS Segment带滑动条切换效果的具体代码,供大家参考,具体内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#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