iOS Segment带滑动条切换效果

 更新时间:2022年03月21日 11:06:09   作者:长沙火山  
这篇文章主要为大家详细介绍了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中NSPredicate谓词的使用

    iOS中NSPredicate谓词的使用

    这篇文章主要给大家介绍了关于iOS中NSPredicate谓词的使用方法,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-01-01
  • 详解iOS App开发中UIViewController的loadView方法使用

    详解iOS App开发中UIViewController的loadView方法使用

    这篇文章主要介绍了详解iOS App开发中UIViewController的loadView方法使用,讲解了访问view属性时loadView方法的调用及使用loadView时的一些注意点,需要的朋友可以参考下
    2016-03-03
  • iOS 11 safeArea详解及iphoneX 适配

    iOS 11 safeArea详解及iphoneX 适配

    本篇文章主要介绍了iOS 11 safeArea详解及iphoneX 适配,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-02-02
  • ios UITableView 自定义右滑删除的实现代码

    ios UITableView 自定义右滑删除的实现代码

    这篇文章主要介绍了ios UITableView 自定义右滑删除的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • iOS 数据结构之数组的操作方法

    iOS 数据结构之数组的操作方法

    这篇文章主要介绍了iOS 数据结构之数组的操作方法,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
    2018-07-07
  • IOS URL中文乱码问题解决方案

    IOS URL中文乱码问题解决方案

    这篇文章主要介绍了IOS 解决URL中文乱码问题解决方案的相关资料,需要的朋友可以参考下
    2016-10-10
  • Mac OSX 打开原生自带读写NTFS功能(图文)

    Mac OSX 打开原生自带读写NTFS功能(图文)

    这篇文章主要介绍了Mac OSX 打开原生自带读写NTFS功能(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • iOS应用开发中运用设计模式中的组合模式的实例解析

    iOS应用开发中运用设计模式中的组合模式的实例解析

    这篇文章主要介绍了iOS应用开发中运用设计模式中的组合模式的实例解析,示例代码为传统的Objective-C,需要的朋友可以参考下
    2016-03-03
  • iOS 通用链接(Universal Link)配置详解

    iOS 通用链接(Universal Link)配置详解

    这篇文章主要介绍了iOS 通用链接(Universal Link)配置详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • 详解iOS应用中播放本地视频以及选取本地音频的组件用法

    详解iOS应用中播放本地视频以及选取本地音频的组件用法

    这里来为大家详解iOS应用中播放本地视频以及选取本地音频的组件用法,分别使用MPMoviePlayerControlle和MPMediaPickerController来实现,两个都是MediaPlayer.framework中的多媒体组件,所以我们放到一起来讲.
    2016-06-06

最新评论