在iOS开发过程中,分段控制器的使用频率还是蛮高的,下面是我写的一个简单的demo,大家可以把代码直接复制过去,就可以使用,ios9最新支持。

//
// ViewController.m
// 03_Segmentedcontrol_Test
//
// Created by 博爱之家 on 15/12/1.
// Copyright © 2015年 博爱之家. All rights reserved.
// #import "ViewController.h" //宏定义
//当前设备的屏幕宽度
#define KSCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width //当前设备的屏幕高度
#define KSCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height // 颜色
#define COLOR_C(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A] @interface ViewController () @property (nonatomic, strong) UIView *subView;
@property (nonatomic, strong) UIView *currentView;
@property (nonatomic, strong) UISegmentedControl *segmentedControl;
//滚动的下划线
@property (nonatomic,strong)UIView *lineView; @property (nonatomic, assign) BOOL isView1; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; // 创建子视图
CGRect frame = CGRectMake(, + , KSCREEN_WIDTH, KSCREEN_HEIGHT - );
_subView = [[UIView alloc] initWithFrame:frame];
[self.view addSubview:_subView]; // 设置分段控制器
[self setUpSegmentedControl]; _isView1 = YES;
[self enterView1]; // 添加下划线
self.lineView = [[UIView alloc] initWithFrame:CGRectMake(, +, KSCREEN_WIDTH/, )];
self.lineView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_lineView];
} #pragma mark 设置分段控制器
- (void)setUpSegmentedControl
{
self.segmentedControl = [[UISegmentedControl alloc ]initWithItems:@[@"标题1",@"标题2",@"标题3"]];
self.segmentedControl.frame = CGRectMake(, , KSCREEN_WIDTH, );
[self.view addSubview:self.segmentedControl]; //设置控件的颜色
self.segmentedControl.tintColor = COLOR_C(, , , 1.0); //默认选中的索引
self.segmentedControl.selectedSegmentIndex = ; // 字体颜色
NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],NSForegroundColorAttributeName,[UIFont fontWithName:@"AppleGothic"size:],NSFontAttributeName ,nil];
NSDictionary *dict2 = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor orangeColor],NSForegroundColorAttributeName,[UIFont fontWithName:@"AppleGothic"size:],NSFontAttributeName ,nil]; [self.segmentedControl setTitleTextAttributes:dict1 forState:UIControlStateNormal];
[self.segmentedControl setTitleTextAttributes:dict2 forState:UIControlStateSelected]; //设置在点击后是否恢复原样
_segmentedControl.momentary = NO; // 点击事件
[_segmentedControl addTarget:self action:@selector(controllerPressed:) forControlEvents:UIControlEventValueChanged];
} #pragma mark - 点击事件
- (IBAction)controllerPressed:(UISegmentedControl *)sender
{
self.lineView.hidden = NO; switch ([sender selectedSegmentIndex])
{
case :
{
CGRect lineFrame = self.lineView.frame;
lineFrame.origin.x = ; [UIView animateWithDuration:0.2 animations:^{
self.lineView.frame = lineFrame;
}];
[self enterView1];
}
break;
case :
{
CGRect frame = self.lineView.frame;
frame.origin.x = KSCREEN_WIDTH/; [UIView animateWithDuration:0.2 animations:^{ self.lineView.frame = frame;
}]; [self enterView2];
}
break;
case :
{
//给下划线 view 添加动画
CGRect lineFrame = self.lineView.frame;
lineFrame.origin.x = KSCREEN_WIDTH/ * ; [UIView animateWithDuration:0.2 animations:^{ self.lineView.frame = lineFrame;
}];
[self enterView3];
}
break; default:
break;
}
NSLog(@"Segment %ld selected\n", (long)sender.selectedSegmentIndex);
} - (void)enterView1
{
_isView1 = YES;
UIView *view1 = [[UIView alloc] initWithFrame:self.subView.bounds];
[self.currentView removeFromSuperview];
view1.backgroundColor = [UIColor yellowColor];
self.currentView = view1;
[self.subView addSubview:view1];
} - (void)enterView2
{
_isView1 = NO;
UIView *view2 = [[UIView alloc] initWithFrame:self.subView.bounds];
[self.currentView removeFromSuperview];
view2.backgroundColor = [UIColor blueColor];
self.currentView = view2;
[self.subView addSubview:view2];
} - (void)enterView3
{
_isView1 = NO;
UIView *view3 = [[UIView alloc] initWithFrame:self.subView.bounds];
[self.currentView removeFromSuperview];
view3.backgroundColor = [UIColor greenColor];
self.currentView = view3;
[self.subView addSubview:view3];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

最后的效果如下:

iOS 学习笔记三【segmentedControl分段控制器详细使用方法】的更多相关文章

  1. Spark学习笔记-三种属性配置详细说明【转】

    相关资料:Spark属性配置  http://www.cnblogs.com/chengxin1982/p/4023111.html 本文出处:转载自过往记忆(http://www.iteblog.c ...

  2. iOS 学习笔记 三 (2015.03.05)

    服务和特征都是用UUID来唯一标识的,UUID的概念如果不清楚请自行google,国际蓝牙组织为一些很典型的设备(比如测量心跳和血压的设备)规定了标准的service UUID(特征的UUID比较多, ...

  3. [转]IOS 学习笔记(8) 滚动视图(UIScrollView)的使用方法

    下面介绍pageControl结合ScrollView实现连续滑动翻页的效果,ScrollView我们在应用开发中经常用到,以g这种翻页效果还是很好看的,如下图所示: 通过这个例子,我们重点学习UIS ...

  4. iOS学习笔记之UITableViewController&UITableView

    iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...

  5. iOS学习笔记-自己动手写RESideMenu

    代码地址如下:http://www.demodashi.com/demo/11683.html 很多app都实现了类似RESideMenu的效果,RESideMenu是Github上面一个stars数 ...

  6. Oracle学习笔记三 SQL命令

    SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)  

  7. angular学习笔记(三十)-指令(5)-link

    这篇主要介绍angular指令中的link属性: link:function(scope,iEle,iAttrs,ctrl,linker){ .... } link属性值为一个函数,这个函数有五个参数 ...

  8. IOS学习笔记48--一些常见的IOS知识点+面试题

      IOS学习笔记48--一些常见的IOS知识点+面试题   1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...

  9. iOS学习笔记-自定义过渡动画

    代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...

随机推荐

  1. Problem M: 输出九九乘法表

    #include<stdio.h> int main() { int n,i,j; scanf("%d",&n); n>=&&n<= ...

  2. C#Windows服务:一些方法(启动、停止等)

    前面讲述了如何创建和安装服务(创建windows服务),下面把启动.停止.判断是否启动的方法也写一下. /// <summary> /// 判断是否安装了某个服务 /// </sum ...

  3. 我的vim配置---jeffy-vim-v2.1.tar

    http://files.cnblogs.com/pengdonglin137/jeffy-vim-v2.1.rar 使用方法: 在Linux下,解压后,进入解压后的目录,执行./install.sh ...

  4. Matlab与C++混合编程 编写独立外部应用程序时出现“无法定位序数3906于动态链接库LIBEAY32.dll上”错误

    出现“无法定位序数3906于动态链接库LIBEAY32.dll上”错误,这种错误一般是同名函数出现在两个不同的头文件中了. 笔者的这个错误是由于 #include "mat.h" ...

  5. T-sql脚本规范

    一.创建表 IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'表名') AND type in (N'U') ...

  6. iOS -- 解决iOS11中navigationBar上使用initWithCustomView按钮图片错位 frame无效

    在iOS11上当使用如下代码设置时 UIButton *shareButton = [UIButton buttonWithType:(UIButtonTypeCustom)]; shareButto ...

  7. servlet虚拟路径映射

    在web.xml文件中,一个<servlet-mapping>元素用于映射一个Servlet的对外访问路径,该路径也称为虚拟路径.例如<url-pattern>/TestSer ...

  8. 主流浏览器CSS3和HTML5兼容性

    各大主流浏览器对 CSS3 和 HTML5 的支持越来越完善,曾经让多少前端开发人员心碎的IE系也开始拥抱标准.就在前几天,W3C的 HTML5 社区领袖 Shelley 宣布,HTML5的开发工作已 ...

  9. Netty源码细节-accept、read(Linux os层 + Netty层代码细节)(转)

    原文:http://budairenqin.iteye.com/blog/2215899 这篇分析一下accept的细节, 我觉得网络IO相关开发很多时候不能仅仅局限于java层, 尤其从accept ...

  10. C#异常处理机制初步

    原地址:http://www.sudu.cn/info/html/edu/net/20071227/50446.html 一.c#的异常处理所用到关键字 try 用于检查发生的异常,并帮助发送任何可能 ...