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 ...
随机推荐
- Github上的iOS资料-个人记录
动画 awesome-ios-animation收集了iOS平台下比较主流炫酷的几款动画框架 RCTRefreshControlqq的下拉刷新 TBIconTransitionKiticon 的点击动 ...
- iOS开发技巧——关闭Autoresizing开启Autolayout
在使用Autolayout时有时候会有莫名其妙的错误,是因为UIView是默认同时开启Autoresizing和Autolayout的. 但我们的Autolayout很容易和Autoresizing冲 ...
- 如何提高码农产量,基于ASP.NET MVC的敏捷开发框架之工作流开发随笔三
前言 “厂长,APP的那几个功能都差不多了,接下来要做工作流,工作流这东西我完全没概念啊.” “查尔斯,一般来说工作流就是指将指定的数据.文件.任务按照预定的规则进行传递流转.比如说你要请假,拿个请假 ...
- 【java】递归统计本地磁盘所有文件,提取重复文件,JDK8 map迭代
package com.sxd.createDao; import java.io.File; import java.time.LocalDateTime; import java.util.Has ...
- 微信开发之自定义菜单--weixin-java-tools
一.前言 平时在开发微信的过程中,肯定会设计到微信的相关菜单的使用,这次就和大家介绍下如何使用weixin-java-tools来管理菜单 二.自定义菜单分类 1.click:点击推事件用户点击cli ...
- Array.apply 方法的使用
Array.apply(null, {length: 5}) length为特殊字段,意思是生成一个长度为5的数组,由于没赋值,所以都是undefined; 如果要赋值,可以这样 console.lo ...
- 一天干掉一只Monkey计划(序)【转】
http://www.cnblogs.com/Zephyroal/archive/2011/10/10/2206509.html 一天干掉一只Monkey计划(序) 一天干掉一只Monkey计划(序) ...
- 【转】es6的拓展运算符 spread ...
原文:https://blog.csdn.net/qq_30100043/article/details/53391308 The rest parameter syntax allows us to ...
- [Spring boot] Configuring and Accessing a Data Source
We need our data persistence with configuring our datasouce: In application.properties: spring.h2.co ...
- Android API Guides---Host-based Card Emulation
Host-based Card Emulation 很多提供NFC功能的Android手机已经支持NFC卡模拟.在大多数情况下.该卡是由在该装置的单独芯片仿真,称为安全元件. 无线运营商提供了非常 ...