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 ...
随机推荐
- 1.6(学习笔记)EL表达式
1.表达式输出属性 先来看一个简单的表达式小例子 el.jsp <%@ page language="java" contentType="text/html; c ...
- 6.5(java学习笔记)其他流(字节数组流,数据流,对象流,打印流)
一.字节数组流 之前使用输入输出流的操作的对象是文件,而这里字节数组流操作的对象是内存,内存可以看做是一个字节数组. 使用字节数组流读写就可以看做是从内存A到内存B的读写,对象时内存即字节数组. 1. ...
- [SourceTree]--记录Win10 安装SourceTree免注册登陆
记录SourceTree一次安装不成功的过程及解决办法 SourceTree简介 按照官网介绍:SourceTree是一款用于Windows和Mac的免费Git客户端.简化了用户与Git存储(仓)库的 ...
- 如何在debug模式下,使用正式的签名文件
有两种方式(在集成第三方库的使用 使用的非常多) 签名配置信息 一是直接按F4,在项目结构面板中进行设置,只要操作两个两个选项卡就好了,signing(生成配置信息)和build types(打包类 ...
- Kubernetes ServiceAccount的配置
开始配置Kubernetes集群的时候为了少出问题,都是在apiserver配置中去掉ServiceAccount采用非安全连接的方式,但在后面配置FEK日志的过程中,很多时候绕不开这个安全机制,但因 ...
- Spark(三) -- Shark与SparkSQL
首先介绍一下Shark的概念 Shark简单的说就是Spark上的Hive,其底层依赖于Hive引擎的 但是在Spark平台上,Shark的解析速度是Hive的几多倍 它就是Hive在Spark上的体 ...
- [TypeScript] Dynamically Allocate Function Types with Conditional Types in TypeScript
Conditional types take generics one step further and allow you to test for a specific condition, bas ...
- TestNG+ReportNG+Maven优化测试报告
转载:https://www.cnblogs.com/hardy-test/p/5354733.html 首先在eclipse里面创建一个maven项目,具体要配置maven环境,请自行百度搭配环境. ...
- Quartz定时框架入门
Quartz框架是Java开源的定时任务调度器,Quartz框架中有如下核心概念: 1. Job 任务接口,接口中只声明方法void execute(JobExecutionContext conte ...
- HDU4499 Cannon DFS 回溯的应用
题意就是给你一个n*m的棋盘,然后上面已经有了 棋子.并给出这些棋子的坐标,可是这些棋子是死的就是不能动,然后让你在棋盘上面摆炮.可是炮之间不能互相吃.吃的规则我们斗懂得 炮隔山打嘛.问你最多能放几个 ...