用标签页TitleSwitch切换不通的控制器
用标签页TitleSwitch切换不通的控制器
教程效果:
项目开发中效果:
各种源码:
TitleSwitch.h 与 TitleSwitch.m (这个是修改过的升级版本)
//
// TitleSwitch.h
// TitleSwitch
//
// Created by YouXianMing on 14/11/4.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h> /**
* 使用细节
* TitleSwitch *titleSwitch = [[TitleSwitch alloc] initWithFrame:CGRectMake(0, 100, 320, 40)];
titleSwitch.titles = @[@"YouXianMing", @"NoZuoNoDie", @"BlueShit"];
titleSwitch.titleFont = [UIFont systemFontOfSize:15.f];
titleSwitch.lineWidth = 1.f;
titleSwitch.canTouchOnlyButtonOneTime = YES;
titleSwitch.delegate = self;
[titleSwitch createTitleSwitchView]; [self.view addSubview:titleSwitch]; *
*
*/ @protocol TitleSwitchDelegate <NSObject>
@optional
- (void)willSelectIndex:(NSInteger)index;
- (void)didSelectIndex:(NSInteger)index;
@end @interface TitleSwitch : UIView /**
* 协议
*/
@property (nonatomic, assign) id<TitleSwitchDelegate> delegate; /**
* 作为按钮的标题
*/
@property (nonatomic, strong) NSArray *titles; /**
* 线的宽度
*/
@property (nonatomic, assign) CGFloat lineWidth; /**
* 线的颜色
*/
@property (nonatomic, strong) UIColor *lineColor; /**
* 标题字体
*/
@property (nonatomic, strong) UIFont *titleFont; /**
* 普通标题颜色
*/
@property (nonatomic, strong) UIColor *normalTitleColor; /**
* 选中标题的颜色
*/
@property (nonatomic, strong) UIColor *selectedTitleColor; /**
* 一次只能按一个按钮触发动画效果
*/
@property (nonatomic, assign) BOOL canTouchOnlyButtonOneTime; /**
* 开启按钮点击时高亮颜色的效果 & 高亮颜色
*/
@property (nonatomic, assign) BOOL enableButtonTitleHighlighted;
@property (nonatomic, strong) UIColor *highlightedTitleColor; /**
* 小横线条的X方向的位移值(可以设置动画)
*/
@property (nonatomic, assign) CGFloat linePositionX;
/**
* 根据linePositionX重设标题颜色
*/
- (void)resetTilte; /**
* 创建TitleSwitch的view出来
*/
- (void)createTitleSwitchView; @end
//
// TitleSwitch.m
// TitleSwitch
//
// Created by YouXianMing on 14/11/4.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "TitleSwitch.h" typedef enum : NSUInteger {
NORMAL_BUTTON = 0x11,
LINE_VIEW = 0x1122,
} ENUM_VIEWTAG; @implementation TitleSwitch - (void)createTitleSwitchView { // 如果没有title,则直接返回
if (_titles.count == ) {
return;
} // 获取尺寸
CGFloat frameWidth = self.bounds.size.width;
CGFloat frameHeight = self.bounds.size.height; // 计算按钮的宽度&高度
CGFloat buttonWidth = frameWidth / _titles.count;
CGFloat buttonHeight = ;
CGFloat defaultLineWidth = .f;
if (_lineWidth == ) {
buttonHeight = frameHeight - defaultLineWidth; // 默认线条占用一个像素
} else {
buttonHeight = frameHeight - _lineWidth;
} // 初始化所有按钮
for (int i = ; i < _titles.count; i++) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(buttonWidth * i,
,
buttonWidth,
buttonHeight)];
button.tag = NORMAL_BUTTON + i;
[self addSubview:button]; [button setTitle:_titles[i] forState:UIControlStateNormal]; // 普通颜色
if (i == ) {
[self selectButtonStyle:button];
} else {
[self normalButtonStyle:button];
} // 高亮颜色
if (_enableButtonTitleHighlighted == YES && _highlightedTitleColor) {
[button setTitleColor:_highlightedTitleColor forState:UIControlStateHighlighted];
} // 添加事件
[button addTarget:self action:@selector(buttonsEvent:) forControlEvents:UIControlEventTouchUpInside]; // 设置字体
if (_titleFont) {
button.titleLabel.font = _titleFont;
}
} // 初始化横线view
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(, buttonHeight + , buttonWidth,
(_lineWidth == ? defaultLineWidth : _lineWidth))];
lineView.tag = LINE_VIEW;
[self addSubview:lineView];
if (_lineColor) {
lineView.backgroundColor = _lineColor;
} else {
lineView.backgroundColor = [UIColor redColor];
}
} /**
* 按钮事件
*
* @param button 触摸事件中的按钮
*/
- (void)buttonsEvent:(UIButton *)button {
// 获取到lineView
UIView *lineView = [self viewWithTag:LINE_VIEW]; // 哪一个button
NSInteger whichButton = button.tag - NORMAL_BUTTON; // 计算按钮的宽度&高度
CGFloat frameWidth = self.bounds.size.width;
CGFloat buttonWidth = frameWidth / _titles.count; [[self subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
UIButton *tmp = (UIButton *)obj;
if ([tmp isKindOfClass:[UIButton class]]) {
if (tmp == button) {
[self selectButtonStyle:tmp];
} else {
[self normalButtonStyle:tmp];
}
}
}]; // 做动画
if (_canTouchOnlyButtonOneTime == YES) {
self.userInteractionEnabled = NO;
} if (_delegate && [_delegate respondsToSelector:@selector(willSelectIndex:)]) {
[_delegate willSelectIndex:whichButton];
} [UIView animateWithDuration:0.25f animations:^{
CGRect rect = lineView.frame;
rect.origin.x = whichButton * buttonWidth;
lineView.frame = rect;
} completion:^(BOOL finished) {
if (_canTouchOnlyButtonOneTime == YES) {
self.userInteractionEnabled = YES;
} if (_delegate && [_delegate respondsToSelector:@selector(didSelectIndex:)]) {
[_delegate didSelectIndex:whichButton];
}
}];
} /**
* 选中按钮的样式
*
* @param button 按钮
*/
- (void)selectButtonStyle:(UIButton *)button { if (_selectedTitleColor) {
[button setTitleColor:_selectedTitleColor
forState:UIControlStateNormal];
} else {
[button setTitleColor:[UIColor redColor]
forState:UIControlStateNormal];
}
} /**
* 普通按钮样式
*
* @param button 按钮
*/
- (void)normalButtonStyle:(UIButton *)button { if (_normalTitleColor) {
[button setTitleColor:_normalTitleColor
forState:UIControlStateNormal];
} else {
[button setTitleColor:[UIColor colorWithRed:0.369 green:0.369 blue:0.369 alpha:]
forState:UIControlStateNormal];
}
} - (void)resetTilte { // 获取尺寸
CGFloat frameWidth = self.bounds.size.width; // 计算按钮的宽度&高度
CGFloat buttonWidth = frameWidth / _titles.count; // 获取到位置
NSInteger index = _linePositionX / buttonWidth + ; [[self subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { UIButton *tmp = (UIButton *)obj;
if ([tmp isKindOfClass:[UIButton class]]) {
if (idx == index) {
[self selectButtonStyle:tmp];
} else {
[self normalButtonStyle:tmp];
}
}
}];
} #pragma mark - 重写各种setter,getter方法
@synthesize linePositionX = _linePositionX;
- (void)setLinePositionX:(CGFloat)linePositionX {
_linePositionX = linePositionX; // 获取到lineView
UIView *lineView = [self viewWithTag:LINE_VIEW];
if (lineView) {
CGRect rect = lineView.frame;
rect.origin.x = linePositionX;
lineView.frame = rect;
}
}
- (CGFloat)linePositionX {
return _linePositionX;
} @end
WxHxD.h 与 WxHxD.m
//
// WxHxD.h
// PM2.5
//
// Created by YouXianMing on 14/10/29.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <Foundation/Foundation.h> @interface WxHxD : NSObject /**
* 应用程序中可以放置view的区间的高度(部分区域会被导航栏控制器的高度占有而不计算在类)
*
* @return 高度
*/
+ (CGFloat)appHeight; /**
* 屏幕高度
*
* @return 屏幕高度
*/
+ (CGFloat)screenHeight; /**
* 屏幕宽度
*
* @return 屏幕宽度
*/
+ (CGFloat)screenWidth; /**
* 状态栏高度
*
* @return 状态栏高度
*/
+ (CGFloat)statusBarHeight; /**
* 导航栏控制器的高度
*
* @return 导航栏控制器的高度
*/
+ (CGFloat)navigationBarHeight; /**
* 标签栏控制器的高度
*
* @return 标签栏控制器的高度
*/
+ (CGFloat)tabbarHeight; /**
* 状态栏与导航栏控制器一起的高度
*
* @return 状态栏与导航栏控制器一起的高度
*/
+ (CGFloat)statusBarAndNavigationBarHeight; @end
//
// WxHxD.m
// PM2.5
//
// Created by YouXianMing on 14/10/29.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "WxHxD.h" @implementation WxHxD + (CGFloat)appHeight {
return [UIScreen mainScreen].applicationFrame.size.height;
} + (CGFloat)screenHeight {
return [UIScreen mainScreen].bounds.size.height;
} + (CGFloat)screenWidth {
return [UIScreen mainScreen].bounds.size.width;
} + (CGFloat)statusBarHeight {
return .f;
} + (CGFloat)navigationBarHeight {
return .f;
} + (CGFloat)tabbarHeight {
return .f;
} + (CGFloat)statusBarAndNavigationBarHeight {
return (.f + .f);
} @end
控制器源码:
//
// FirstController.m
// SwitchController
//
// Created by YouXianMing on 14/11/5.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "FirstController.h" @interface FirstController () @end @implementation FirstController - (void)viewDidLoad {
[super viewDidLoad]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
label.center = self.view.center;
label.text = @"FirstController";
label.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:label];
} @end
//
// SecondController.m
// SwitchController
//
// Created by YouXianMing on 14/11/5.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "SecondController.h" @interface SecondController () @end @implementation SecondController - (void)viewDidLoad {
[super viewDidLoad]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
label.center = self.view.center;
label.text = @"SecondController";
label.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:.f];
label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:label];
} @end
主控制器源码:
//
// ViewController.m
// SwitchController
//
// Created by YouXianMing on 14/11/5.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "TitleSwitch.h"
#import "WxHxD.h" #import "FirstController.h"
#import "SecondController.h" @interface ViewController ()<TitleSwitchDelegate, UIScrollViewDelegate> {
TitleSwitch *titleSwitch;
} @property (nonatomic, strong) UIScrollView *mainScrollView; // 滑动用的ScrollView @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 隐藏状态栏(需要先设置plist文件)
[UIApplication sharedApplication].statusBarHidden = YES; // 关闭状态栏 // 初始化UIScrollView
[self initScrollView]; // 初始化标签控制器
[self initTitleSwitch]; // 初始化两个控制器
FirstController *first = [FirstController new];
[self addChildViewController:first]; // 将控制器first变为当前控制器的子控制器
CGRect firstRect = first.view.frame;
firstRect.origin.x = [WxHxD screenWidth] * ; // view值偏移量为 0*screenWidth
first.view.frame = firstRect; SecondController *second = [SecondController new];
[self addChildViewController:second]; // 将控制器second变为当前控制器的子控制器
CGRect secondRect = second.view.frame;
secondRect.origin.x = [WxHxD screenWidth] * ; // view值偏移量为 1*screenWidth
second.view.frame = secondRect; // 加载控制器的view
[_mainScrollView addSubview:first.view];
[_mainScrollView addSubview:second.view];
} /**
* 初始化UIScrollView
*/
- (void)initScrollView {
// 当前显示艺术品的控制器
_mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
_mainScrollView.contentSize = CGSizeMake([WxHxD screenWidth] * , [WxHxD screenHeight]);
_mainScrollView.pagingEnabled = YES;
_mainScrollView.delegate = self;
_mainScrollView.bounces = NO;
_mainScrollView.showsHorizontalScrollIndicator = NO;
[self.view addSubview:_mainScrollView];
} #pragma mark - UIScrollView的代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
titleSwitch.linePositionX = scrollView.contentOffset.x / .f;
} - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[titleSwitch resetTilte];
} /**
* 初始化标签控制器
*/
- (void)initTitleSwitch {
// 标题切换用
titleSwitch = [[TitleSwitch alloc] initWithFrame:CGRectMake(, , [WxHxD screenWidth], )];
titleSwitch.tag = 0x112244; // 添加灰色的线条
UIView *grayLine = [[UIView alloc] initWithFrame:CGRectMake(, , [WxHxD screenWidth], )];
grayLine.backgroundColor = [UIColor colorWithRed:0.827 green:0.827 blue:0.827 alpha:];
[titleSwitch addSubview:grayLine]; titleSwitch.backgroundColor = [UIColor colorWithRed:0.949 green:0.949 blue:0.949 alpha:];
titleSwitch.selectedTitleColor = [UIColor colorWithRed:0.843 green:0.000 blue:0.000 alpha:];
titleSwitch.titles = @[@"YouXianMing",
@"NoZuoNoDie"];
titleSwitch.lineWidth = .f;
titleSwitch.titleFont = [UIFont systemFontOfSize:.f];
titleSwitch.canTouchOnlyButtonOneTime = YES;
titleSwitch.delegate = self;
[titleSwitch createTitleSwitchView];
[self.view addSubview:titleSwitch];
} #pragma mark - TitleSwitch的代理
- (void)willSelectIndex:(NSInteger)index {
if (index == ) { CGPoint point = _mainScrollView.contentOffset;
point.x = ;
[UIView animateWithDuration:0.25f animations:^{
_mainScrollView.contentOffset = point;
}]; [self bringTitleToFrount]; } else {
CGPoint point = _mainScrollView.contentOffset;
point.x = [WxHxD screenWidth];
[UIView animateWithDuration:0.25f animations:^{
_mainScrollView.contentOffset = point;
}]; [self bringTitleToFrount];
}
} /**
* 将标题提到最前面
*/
- (void)bringTitleToFrount {
UIView *tmpView = [self.view viewWithTag:0x112244];
[self.view bringSubviewToFront:tmpView];
} @end
解析:
用标签页TitleSwitch切换不通的控制器的更多相关文章
- selenium WebDriver 对浏览器标签页的切换
关于selenium WebDriver 对浏览器标签页的切换,现在的市面上最新的浏览器,当点击一个链接打开一个新的页面都是在浏览器中打开一个标签页,而selenium只能对窗口进行切换的方法,只能操 ...
- 标签页的切换方法(DOM)
效果: 1.点击“JAVA语言” 2.点击“C语言” 3.点击C++语言 代码: <!doctype html> <html> <head> <meta ch ...
- selenium多个标签页的切换(弹出新页面的切换)
1_windows = driver.current_window_handle #定位当前页面句柄 all_handles = driver.window_handles #获取全部页面句柄 for ...
- bootstrap 标签页tab切换js(含报错原因)
booststrap 标签页的tab切换,相信大家已经都很熟悉了,在boot官网示例以及其他网站已经很多罗列相关代码的了,这里就不赘述了.这里主要贴下让boot标签页默认显示哪个标签页的js. 主要留 ...
- 在Bootstrap开发中解决Tab标签页切换图表显示问题
在做响应式页面的时候,往往需要考虑更多尺寸设备的界面兼容性,一般不能写死像素,以便能够使得界面元素能够根据设备的不同进行动态调整,但往往有时候还是碰到一些问题,如Tab标签第一页面正常显示,但是切换其 ...
- JavaScript 实现 标签页 切换效果
JavaScript 实现 标签页 切换效果 版权声明:未经授权,严禁分享! 构建主体界面 HTML 代码 <h1>实现标签页的切换效果</h1> <ul id=&quo ...
- 在QMainWindow中利用多个QDockWidget构成标签页tab(原创)
功能描述: 在QMainWindow下,使用多个QDockWidget构成可切换,可拖动,可关闭的标签页:标签页的切换由相关联的QAction触发. 实现效果: 代码如下: QDockWidget * ...
- Bootstrap入门(二十七)JS插件4:标签页
Bootstrap入门(二十七)JS插件4:标签页 标签页的切换可以带动内容的变化 首先我们引入CSS文件 <link href="bootstrap.min.css" re ...
- Vim技能修炼教程(9) - 缓冲区和标签页
缓冲区 上一节我们学习了窗口相关的命令,其实多窗口的最大功能在于存放多个不同的缓冲区. 文件载入内存之后,我们操作的其实是缓冲区.:write命令就是将缓冲区写回文件的目的. 查看内存中的缓冲区 通过 ...
随机推荐
- Linux-(ps,grep)
grep命令 1.命令格式: grep [option] pattern file 2.命令功能: 用于过滤/搜索的特定字符.可使用正则表达式能多种命令配合使用,使用上十分灵活. Linux系统中gr ...
- 笔记四:python乱码深度剖析二
一:学习内容 获取更改系统编码 判断字符的编码类型 文件存储和读取的编码 二:获取更改系统编码 1. 获取系统编码 import sys print sys.getdefaultencoding() ...
- rpm使用方法
查看rpm信息:rpm -q [软件的rpm名字]rpm -q下还有很多选项,具体功能如下:rpm -qa 列出所有已安装的RPM文件rpm -qa | grep [rp ...
- JAVA GUID
import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.Exec ...
- [HNOI 2018]毒瘤
Description 题库链接 给出一个 \(n\) 个点 \(m\) 条边的无向图.求独立集个数. \(n\leq 10^5,n-1\leq m\leq n+10\) Solution 由于返祖边 ...
- mac系统终端sudo免输入密码技能get
1.需要在/etc/sudoers中配置. 这个文件的权限是r/r/n,配置之前需要加写权限. sudo chmod u-w /etc/sudoers 2.打开命令窗口sudo visudo 或者 s ...
- Xcode DEBUG宏定义,debug和release模式的选择
设置DEBUG, 使用宏定义: #if DEBUG NSLog(@"debug model"); #else //执行release模式的代码 #endif
- dubbo基于tcp协议的RPC框架
什么是 RPC 框架 谁能用通俗的语言解释一下什么是 RPC 框架? - 远程过程调用协议RPC(Remote Procedure Call Protocol) 首先了解什么叫RPC,为什么要RPC, ...
- CodeForces760B
B. Frodo and pillows time limit per test:1 second memory limit per test:256 megabytes input:standard ...
- 【学习笔记】--- 老男孩学Python,day15 python内置函数大全,递归,二分法
1. lamda匿匿名函数2. sorted()3. filter()4. map()5. 递归函数 一. lamda 匿名函数 为了了解决一些简单的需求⽽设计的⼀句话函数 语法: 函数名 = lam ...