设计标签选择器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> @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; /**
* 创建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 (_normalTitleColor) {
[button setTitleColor:_normalTitleColor
forState:UIControlStateNormal];
} else {
[button setTitleColor:[UIColor redColor]
forState:UIControlStateNormal];
}
} /**
* 普通按钮样式
*
* @param button 按钮
*/
- (void)normalButtonStyle:(UIButton *)button { if (_selectedTitleColor) {
[button setTitleColor:_selectedTitleColor
forState:UIControlStateNormal];
} else {
[button setTitleColor:[UIColor colorWithRed:0.369 green:0.369 blue:0.369 alpha:]
forState:UIControlStateNormal];
}
} @end

使用:

//
// ViewController.m
// TitleSwitch
//
// Created by YouXianMing on 14/11/4.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ViewController.h"
#import "TitleSwitch.h" @interface ViewController ()<TitleSwitchDelegate> @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; TitleSwitch *titleSwitch = [[TitleSwitch alloc] initWithFrame:CGRectMake(, , , )];
titleSwitch.titles = @[@"YouXianMing", @"NoZuoNoDie", @"BlueShit"];
titleSwitch.titleFont = [UIFont systemFontOfSize:.f];
titleSwitch.lineWidth = .f;
titleSwitch.canTouchOnlyButtonOneTime = YES;
titleSwitch.delegate = self;
[titleSwitch createTitleSwitchView]; [self.view addSubview:titleSwitch];
} - (void)willSelectIndex:(NSInteger)index {
NSLog(@"willSelectIndex %ld", (long)index);
} - (void)didSelectIndex:(NSInteger)index {
NSLog(@"didSelectIndex %ld", (long)index);
} @end

注意细节:

设计标签选择器TitleSwitch的更多相关文章

  1. CSS标签选择器(二)

    一.CSS选择器概述 1.1.CSS功能 CSS语言具有两个基本功能:匹配和渲染 当浏览器在解析CSS样式时,首先应该确定哪些元素需要渲染,即匹配哪些HTML元素,这个操作由CSS样式中的选择器负责标 ...

  2. jQuery标签选择器

    $(function() { //alert("hello jquery"); //选择器 //id选择器 $("#bt1").click( function( ...

  3. css标签选择器

    /*标签选择器*/ input[type="text"] { width: 60%; } </style>

  4. jquery基本选择器:id选择器、class选择器、标签选择器、通配符选择器

    全栈工程师开发手册 (作者:栾鹏) jquery系列教程1-选择器全解 jquery基本选择器 jquery基本选择器,包括id选择器.class选择器.标签选择器.通配符选择器,同时配合选择器的空格 ...

  5. H5 标签选择器

    08-标签选择器 我是段落 我是段落 我是段落 我是段落 我是段落 我是标题 <!DOCTYPE html> <html lang="en"> <he ...

  6. CSS 标签选择器

    CSS 标签选择器 再<stype>标签内,通过指定输入标签来配置CSS样式 <html> <head> <!-- style 设置头部标签--> &l ...

  7. css之标签选择器

    标签(空格分隔): 标签选择器 选择器定义: 在一个HTML页面中会有很多很多的元素,不同的元素可能会有不同的样式,某些元素又需要设置相同的样式,选择器就是用来从HTML页面中查找特定元素的,找到元素 ...

  8. 前端基础之CSS的引入+HTML标签选择器+CSS操作属性

    clear:left/ringt属性 CSS:语法形式上由选择器+以及一条或多条声明组成:选择器查找到指定的html标签后,使用css属性设置html标签的样式:                   ...

  9. 第三百二十五节,web爬虫,scrapy模块标签选择器下载图片,以及正则匹配标签

    第三百二十五节,web爬虫,scrapy模块标签选择器下载图片,以及正则匹配标签 标签选择器对象 HtmlXPathSelector()创建标签选择器对象,参数接收response回调的html对象需 ...

随机推荐

  1. python中不可变数据类型和可变数据类型

    在学习python过程中我们一定会遇到不可变数据类型和可变数据类型. 1.名词解释 以下所有的内容都是基于内存地址来说的. 不可变数据类型: 当该数据类型的对应变量的值发生了改变,那么它对应的内存地址 ...

  2. tcpdump非常实用的抓包实例

    详细的文档见tcpdump高级过滤技巧 基本语法 ========过滤主机--------- 抓取所有经过 eth1,目的或源地址是 192.168.1.1 的网络数据# tcpdump -i eth ...

  3. centos7-安装mysql5.6.36

    本地安装了mysql5.7, 但和springboot整合jpa时会出现 hibernateException, 不知道为什么, 换个mysql5.6版本的mysql,  源码安装, cmake一直过 ...

  4. 编写代码:ATM的登陆界面(用户验证、主菜单的选择) 查询-- 存款-- 取款-- 退出

    #include <stdio.h>#include <windows.h>int main (void){    int password,one,two,money1=10 ...

  5. 扫描网站服务器真实IP的小脚本

    #!/usr/bin/env python # -*- coding: gbk -*- # -*- coding: utf_8 -*- # Date: 2015年9月11日 # Author:蔚蓝行 ...

  6. Firebird reset SYSDBA password

    Firebird 重置超级管理员SYSDBA密码 首先登陆到服务器上(以下以Windows系统演示),命令行进入安装目录,我这里是 D:\-Installer\-Firebird\Firebird-3 ...

  7. val();html();.text()区别

    对于innerHTML 属性,几乎所有的元素都有innerHTML属性,它是一个字符串,用来设置或获取位于对象起始和结束标签内的HTML.(获取HTML当前标签的起始和结束里面的内容) 对于inner ...

  8. MyEclipse关闭当前正在编辑的页面

    如果要关闭当前正在编辑的页面Ctrl + W 例如: 按下Ctrl + W 只会关闭Const.java这个页面 如果要关闭所有打开的页面Ctrl + Shift + W 例如:按下Ctrl + Sh ...

  9. myBatis组件之缓存实现及使用

    一 .概述 先讲缓存实现,主要是mybatis一级缓存,二级缓存及缓存使用后续补充 Mybatis缓存的实现是基于Map的,从缓存里面读写数据是缓存模块的核心基础功能:除核心功能之外,有很多额外的附加 ...

  10. IDEA下的第一个springBoot

    1.第一步打开File->New->Project,SDK根据自己的需要选择,我这边选的是java7 2.Next之后 设置group 和artifact,根据自己的需要进行修改. 3.导 ...