iOS 学习笔记三【segmentedControl分段控制器详细使用方法】
在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分段控制器详细使用方法】的更多相关文章
- Spark学习笔记-三种属性配置详细说明【转】
相关资料:Spark属性配置 http://www.cnblogs.com/chengxin1982/p/4023111.html 本文出处:转载自过往记忆(http://www.iteblog.c ...
- iOS 学习笔记 三 (2015.03.05)
服务和特征都是用UUID来唯一标识的,UUID的概念如果不清楚请自行google,国际蓝牙组织为一些很典型的设备(比如测量心跳和血压的设备)规定了标准的service UUID(特征的UUID比较多, ...
- [转]IOS 学习笔记(8) 滚动视图(UIScrollView)的使用方法
下面介绍pageControl结合ScrollView实现连续滑动翻页的效果,ScrollView我们在应用开发中经常用到,以g这种翻页效果还是很好看的,如下图所示: 通过这个例子,我们重点学习UIS ...
- iOS学习笔记之UITableViewController&UITableView
iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...
- iOS学习笔记-自己动手写RESideMenu
代码地址如下:http://www.demodashi.com/demo/11683.html 很多app都实现了类似RESideMenu的效果,RESideMenu是Github上面一个stars数 ...
- Oracle学习笔记三 SQL命令
SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)
- angular学习笔记(三十)-指令(5)-link
这篇主要介绍angular指令中的link属性: link:function(scope,iEle,iAttrs,ctrl,linker){ .... } link属性值为一个函数,这个函数有五个参数 ...
- IOS学习笔记48--一些常见的IOS知识点+面试题
IOS学习笔记48--一些常见的IOS知识点+面试题 1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...
- iOS学习笔记-自定义过渡动画
代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...
随机推荐
- iOS键盘监听事件
1.注册键盘通知事件 NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; // 键盘将出现事件监听 [center ...
- microsoft visual studio遇到了问题,需要关闭
http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=13821 装上这个补丁: WindowsXP-KB971513-x86-CH ...
- 连接sqlexpress
sqlexpress在visualstudio安装时可选择安装. 数据源添加 localhost\sqlexpress window身份认证即可.
- <command-line>:0: error: macro names must be identifiers
编译时的出错信息:<command-line>:0: error: macro names must be identifiers 原因: You have a -D flag with ...
- JNI之——在cmd命令行下编译执行C/C++源文件
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46604269 一直用java来敲代码,java配置好jre路径之后.在cmd下编译 ...
- git 出现502错误后用depth一步一步来
公司有个项目的git仓库,因为一些二进制文件也放在里面,版本迭代后,整个仓库特别大,有好几G. 直接git clone是不行的,会报这样的错误: error: RPC failed; HTTP 502 ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-电机实际运行距离跟给定距离不一致怎么办,如何设置Scaling Factor
有时候,让电机从0度转到绝对的360度,有时候会出现电机实际转动更多或者更少的情况. 一般是电机的编码器的Scaling Factor Numerator数值不对导致的,数值越小,则同比转过角度越 ...
- B5:责任链模式 Chain Of Responsibility
使多个对象都有机会处理处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这个对象连成一条链,并沿着该链处理请求,直到有一个对象能够处理它为止. 相当于switch/case,在客户端指定了每一链 ...
- $ gulp watch 运行出错解决方法
$ gulp watch 运行出错解决方法 $ gulp watch 如果你出现了如下报错信息: gulp-notify: [Laravel Elixir] Browserify Fail ...
- JDK自带监控工具 jps、jinfo、jstat、jmap、jconsole
分类: JVM 2010-10-04 11:05 587人阅读 评论(0) 收藏 举报 工具jdkjava远程连接unixstring 常用有五个命令行工具: jinfo: 可以输出并修改运行时的ja ...