服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS Segment带滑动条切换效果

iOS Segment带滑动条切换效果

2022-08-01 11:39长沙火山 IOS

这篇文章主要为大家详细介绍了iOS Segment带滑动条切换,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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

iOS Segment带滑动条切换效果iOS Segment带滑动条切换效果

iOS Segment带滑动条切换效果iOS Segment带滑动条切换效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/u010545480/article/details/47428535

延伸 · 阅读

精彩推荐
  • IOSiOS利用Runtime实现友盟页面数据统计的功能示例

    iOS利用Runtime实现友盟页面数据统计的功能示例

    这篇文章主要给大家介绍了关于iOS利用Runtime实现友盟页面数据统计功能的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的...

    石虎1323932021-04-15
  • IOSiOS中UIActivityIndicatorView的用法及齿轮等待动画实例

    iOS中UIActivityIndicatorView的用法及齿轮等待动画实例

    UIActivityIndicatorView活动指示器最常见的用法便是用来制作那个程序中的齿轮转动的等待效果,接下来我们回来简单整理iOS中UIActivityIndicatorView的用法及齿轮等...

    iOS UI5012021-01-19
  • IOSiOS利用Label实现的简单高性能标签TagView

    iOS利用Label实现的简单高性能标签TagView

    这篇文章主要给大家介绍了关于iOS利用Label实现的简单高性能标签TagView的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的...

    恩说吧7502021-04-22
  • IOSiOS中设置网络超时时间+模拟的方法详解

    iOS中设置网络超时时间+模拟的方法详解

    这篇文章主要介绍了在iOS中设置网络超时时间+模拟的方法,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来跟着小编一起来...

    小小流浪的汉子9622021-03-14
  • IOSIOS 添加自定义字体方法详解

    IOS 添加自定义字体方法详解

    这篇文章主要介绍了IOS 添加自定义字体方法详解的相关资料,需要的朋友可以参考下...

    牵左手不离10492021-01-28
  • IOSIOS UITableView和NavigationBar的常用设置详解

    IOS UITableView和NavigationBar的常用设置详解

    这篇文章主要介绍了IOS UITableView和NavigationBar的常用设置详解的相关资料,需要的朋友可以参考下...

    iOS开发网6802021-03-13
  • IOS如何在iOS上使用MVVM进行路由详解

    如何在iOS上使用MVVM进行路由详解

    这篇文章主要给大家介绍了关于如何在iOS上使用MVVM进行路由的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价...

    敲钟人Quasimodo10372021-05-19
  • IOS详解适配iOS10 的相关权限设置

    详解适配iOS10 的相关权限设置

    在最新版本的iOS10系统中,如果你的项目中访问了隐私数据,比如:相机、相册、录音、定位、联系人等等。涉及到权限问题,本篇文章主要介绍了适配i...

    中文之风7282021-02-24