在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. 【进制转换】CODEVS 1740 进制计算器

    #include<cstdio> #include<iostream> #include<string> using namespace std; string s ...

  2. [HNOI/AHOI2018]寻宝游戏

    题目大意: $n(n\le1000)$个$m(m\le5000)$位的二进制数,第$0$个数为$0$.用$\wedge$和$\vee$将这些数连接起来.$q(q\le1000)$次询问,每次给定一个$ ...

  3. Xcode升级后导致插件不能用, 一句代码更新UUID OK~

      find ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins -name Info.plist -maxdepth 3 | ...

  4. AE 开发添加新增字段 报错

    在为现有要素类新增字段时,有可能会爆出该表正被其他程序或其他人使用,这时我们就会添加失败,解决办法如下: 在新增字段之前,先修改SchemaLock的状态 ISchemaLock pSchemaLoc ...

  5. 【sql】mysql数据库做两条数据替换的操作,不使用第三方变量

    需求: 1.将数据库中两条数据中的唯一约束列  做值的替换 原始思想: 将两条数据查出来,在程序中设置第三方变量,进行两条数据的替换,然后将原始两条数据删除,将新的两条替换后的数据插入. 新思想: 1 ...

  6. JavaScript的map循环、forEach循环、filter循环、reduce循环、reduceRight循环

    1.map循环 let arr=[1,2,3,4]; arr.map(function(value,key,arr){ //值,索引,数组(默认为选定数组) return item; //如果没有re ...

  7. 关于ComboGrid取值为非下拉框数据是,隐藏面板数据清空

    $('#areaGuid').combogrid({ panelWidth: 300, idField: 'guid', textField: 'name', mode: 'remote', meth ...

  8. C#之Hello World(入门 )

    C#是一种简单.现代.面向对象和类型安全的编程语言. C#由C和C++发展而来.C#(英文发音C sharp)牢固地植根于C和C++语言族谱中,是Microsoft专门为使用.NET平台而创建的. • ...

  9. 关于JS里的函数作用域链的总结

    在JavaScript中,函数的作用域链是一个很难理解的东西.这是因为JavaScript中函数的作用域链和其他语言比如C.C++中函数的作用域链相差甚远.本文详细解释了JavaScript中与函数的 ...

  10. 模板:什么是Traits

    Traits不是一种语法特性,而是一种模板编程技巧.Traits在C++标准库,尤其是STL中,有着不可替代的作用.   如何在编译期间区分类型   下面我们看一个实例,有四个类,Farm.Worke ...