定制二选一按钮SwitchButton
定制二选一按钮SwitchButton
效果:
源码:
SwitchButton.h 与 SwitchButton.m
- //
- // SwitchButton.h
- // KongJian
- //
- // Created by YouXianMing on 14/10/24.
- // Copyright (c) 2014年 YouXianMing. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @protocol SwitchButtonDelegate <NSObject>
- - (void)switchButtonState:(BOOL)state;
- @end
- @interface SwitchButton : UIView
- /**
- * 代理
- */
- @property (nonatomic, assign) id<SwitchButtonDelegate> delegate;
- /**
- * 3个view
- */
- @property (nonatomic, strong) UIView *leftView;
- @property (nonatomic, strong) UIView *rightView;
- @property (nonatomic, strong) UIView *blockView;
- /**
- * 动画持续时间
- */
- @property (nonatomic, assign) NSTimeInterval duration;
- /**
- * 块是否在左边
- */
- @property (nonatomic, assign) BOOL blockAtLeft;
- /**
- * 背景颜色
- */
- @property (nonatomic, strong) UIColor *leftBackgroundColor;
- @property (nonatomic, strong) UIColor *rightBackgroundColor;
- /**
- * 便利的创建出按钮
- *
- * @param leftText 左边显示的文本
- * @param rightText 右边显示的文本
- * @param size button的尺寸
- *
- * @return button对象
- */
- + (SwitchButton *)createDefaultTypeButtonWithLeftText:(NSString *)leftText
- rightText:(NSString *)rightText
- size:(CGSize)size;
- @end
- //
- // SwitchButton.m
- // KongJian
- //
- // Created by YouXianMing on 14/10/24.
- // Copyright (c) 2014年 YouXianMing. All rights reserved.
- //
- #import "SwitchButton.h"
- @interface SwitchButton ()
- @property (nonatomic, strong) UIButton *button;
- @property (nonatomic, strong) UIView *leftBackView;
- @property (nonatomic, strong) UIView *rightBackView;
- @property (nonatomic, strong) UIView *blockBackView;
- @end
- @implementation SwitchButton
- - (instancetype)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- self.layer.masksToBounds = YES;
- // 按钮
- _button = [[UIButton alloc] initWithFrame:self.bounds];
- [_button addTarget:self
- action:@selector(buttonEvent)
- forControlEvents:UIControlEventTouchUpInside];
- [self addSubview:_button];
- // 左侧view
- _leftBackView = [[UIView alloc] initWithFrame:CGRectMake(, ,
- self.bounds.size.width / .f,
- self.bounds.size.height)];
- _leftBackView.userInteractionEnabled = NO;
- [self addSubview:_leftBackView];
- // 右侧view
- _rightBackView = [[UIView alloc] initWithFrame:CGRectMake(self.bounds.size.width / .f, ,
- self.bounds.size.width / .f, self.bounds.size.height)];
- _rightBackView.userInteractionEnabled = NO;
- [self addSubview:_rightBackView];
- // 遮挡用的view
- _blockBackView = [[UIView alloc] initWithFrame:CGRectMake(, ,
- self.bounds.size.width / .f,
- self.bounds.size.height)];
- _blockBackView.userInteractionEnabled = NO;
- [self addSubview:_blockBackView];
- }
- return self;
- }
- - (void)buttonEvent {
- if (_blockBackView.frame.origin.x == ) {
- if (_delegate) {
- [_delegate switchButtonState:YES];
- }
- [UIView animateWithDuration:(_duration > ? _duration : 0.2f)
- animations:^{
- _blockBackView.frame = CGRectMake(self.bounds.size.width / .f, ,
- self.bounds.size.width / .f, self.bounds.size.height);
- if (_leftBackgroundColor) {
- _button.backgroundColor = _leftBackgroundColor;
- }
- } completion:^(BOOL finished) {
- }];
- } else {
- [_delegate switchButtonState:NO];
- [UIView animateWithDuration:(_duration > ? _duration : 0.2f)
- animations:^{
- _blockBackView.frame = CGRectMake(, ,
- self.bounds.size.width / .f, self.bounds.size.height);
- if (_rightBackgroundColor) {
- _button.backgroundColor = _rightBackgroundColor;
- }
- } completion:^(BOOL finished) {
- }];
- }
- }
- @synthesize leftView = _leftView;
- - (void)setLeftView:(UIView *)leftView {
- _leftView = leftView;
- leftView.userInteractionEnabled = NO;
- [_leftBackView addSubview:leftView];
- [self bringSubviewToFront:_blockBackView];
- }
- @synthesize rightView = _rightView;
- - (void)setRightView:(UIView *)rightView {
- _rightView = rightView;
- rightView.userInteractionEnabled = NO;
- [_rightBackView addSubview:rightView];
- [self bringSubviewToFront:_blockBackView];
- }
- @synthesize blockView = _blockView;
- - (void)setBlockView:(UIView *)blockView {
- _blockView = blockView;
- blockView.userInteractionEnabled = NO;
- [_blockBackView addSubview:blockView];
- [self bringSubviewToFront:_blockBackView];
- }
- @synthesize blockAtLeft = _blockAtLeft;
- - (void)setBlockAtLeft:(BOOL)blockAtLeft {
- _blockAtLeft = blockAtLeft;
- if (blockAtLeft == YES) {
- _blockBackView.frame = CGRectMake(, ,
- self.bounds.size.width / .f,
- self.bounds.size.height);
- } else {
- _blockBackView.frame = CGRectMake(self.bounds.size.width / .f, ,
- self.bounds.size.width / .f, self.bounds.size.height);
- }
- }
- #pragma mark - 便利构造器
- + (SwitchButton *)createDefaultTypeButtonWithLeftText:(NSString *)leftText
- rightText:(NSString *)rightText
- size:(CGSize)size {
- SwitchButton *button = [[SwitchButton alloc] initWithFrame:CGRectMake(, , size.width, size.height)];
- button.layer.cornerRadius = .f;
- button.blockAtLeft = NO;
- button.leftBackgroundColor = [UIColor colorWithRed:0.969 green:0.365 blue:0.137 alpha:];
- button.backgroundColor = [UIColor colorWithRed:0.969 green:0.365 blue:0.137 alpha:];
- button.rightBackgroundColor = [UIColor colorWithRed:0.278 green:0.835 blue:0.855 alpha:];
- button.duration = 0.3f;
- // 左侧文本
- UILabel *man = [[UILabel alloc] initWithFrame:CGRectMake(, , button.bounds.size.width/.f,
- button.bounds.size.height)];
- man.text = leftText;
- man.textColor = [UIColor whiteColor];
- man.font = [UIFont systemFontOfSize:.f];
- man.textAlignment = NSTextAlignmentCenter;
- button.leftView = man;
- // 右侧文本
- UILabel *woman = [[UILabel alloc] initWithFrame:CGRectMake(, , button.bounds.size.width/.f,
- button.bounds.size.height)];
- woman.text = rightText;
- woman.textColor = [UIColor whiteColor];
- woman.font = [UIFont systemFontOfSize:.f];
- woman.textAlignment = NSTextAlignmentCenter;
- button.rightView = woman;
- // 中间挡住的块
- UIView *blockView = [[UIView alloc] initWithFrame:CGRectMake(, , button.bounds.size.width/.f - ,
- button.bounds.size.height - )];
- blockView.layer.cornerRadius = .f;
- blockView.backgroundColor = [UIColor whiteColor];
- button.blockView = blockView;
- return button;
- }
- @end
使用时候源码:
- //
- // ViewController.m
- // KongJian
- //
- // Created by YouXianMing on 14/10/24.
- // Copyright (c) 2014年 YouXianMing. All rights reserved.
- //
- #import "ViewController.h"
- #import "SwitchButton.h"
- @interface ViewController ()<SwitchButtonDelegate>
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- SwitchButton *button = [SwitchButton createDefaultTypeButtonWithLeftText:@"Y"
- rightText:@"X"
- size:CGSizeMake(, )];
- button.delegate = self;
- button.center = self.view.center;
- [self.view addSubview:button];
- }
- - (void)switchButtonState:(BOOL)state {
- NSLog(@"%d", state);
- }
- @end
核心的地方:
便利构造器(你可以自己去写):
定制二选一按钮SwitchButton的更多相关文章
- 设计多选一按钮ChooseOnlyButton
设计多选一按钮ChooseOnlyButton 效果: 源码: ChooseOnlyButton.h 与 ChooseOnlyButton.m // // ChooseOnlyButton.h // ...
- sencha touch Model validations 自定义验证 二选一输入验证、重复验证、时间验证、比较验证、条件验证(2015-1-14)
项目初始化时执行以下代码 //重写模型,方便进行自定义验证 Ext.define("Ext.zh.data.Model", { override: "Ext.data.M ...
- 设计可以多选的按钮ChooseManyButton
设计可以多选的按钮ChooseManyButton 效果: 源码: ChooseManyButton.h 与 ChooseManyButton.m // // ChooseManyButton.h / ...
- 定制选择范围的按钮RangeButton
定制选择范围的按钮RangeButton 效果: 源码: RangeButton.h 与 RangeButton.m // // RangeButton.h // PulsingView // // ...
- Android笔记-5-EditText密码和Checkbox二选一
EditText密码:明文和密文 密文: public class MainActivity extends Activity { private EditText password = null; ...
- jquery validate 二选一,错误提示在一处
转载自:http://blog.51yip.com/jsjquery/1483.html 有一同事对jquery validate这个插件不熟,实现多处报错信息在一处,并且还有二选一的情况,二个输入框 ...
- 三大视频网站Url的处理保存(视频和图片二选一操作)
前台Js // 视频处理 var textVideoLink=$("input[name='textVideoLink']").val(); // 去除所有有的引号和空格 var ...
- JS流程控制语句 二选一 (if...else语句) 语法: if(条件) { 条件成立时执行的代码} else {条件不成立时执行的代码}
二选一 (if...else语句) if...else语句是在指定的条件成立时执行代码,在条件不成立时执行else后的代码. 语法: if(条件) { 条件成立时执行的代码} else {条件不成立时 ...
- html弹出二选一窗口,然后根据点击执行对应的js方法
html弹出二选一窗口,然后根据点击执行对应的js方法 layer.confirm("我是弹出来的字", {btn:['确认','取消']}, function(){ ...方法1 ...
随机推荐
- C++中内联函数
目录 什么是内联函数 如何使函数内联 为什么要使用内联函数 inline函数的优缺点分析 什么时候该使用内联函数 正文 在C语言中,我们使用宏定义函数这种借助编译器的优化技术来减少程序的执行时间,那么 ...
- prometheus安装、使用
本文主要参考https://songjiayang.gitbooks.io/prometheus/introduction/what.html 二进制包安装 我们可以到 Prometheus 二进制下 ...
- 修改MVC默认的pageBaseType以添加功能
试想下在MVC的前端页面JS或者html中需要使用多语言,而后端的多语言是维护在资源文件中的,前端如果使用的话需要使用AJAX频繁的获取,一个页面中可能会存在大量的需要语言转换的地方,频繁使用AJAX ...
- PowerDesigner设置默认值名称规则
一.需求背景: 使用PowerDesigner创建表时,若设置某列默认值时,自动生成规则的默认值名称.比如说:DF_表名_列名 二.设置步骤: 1.选择Database—>Edit Curren ...
- office中把标题之后的空格去掉
调整列表缩进--编号之后不特别标注可以把标题之后的空格去掉
- Java开发中常用的设计模式(三)---建造者模式
一. 模式结构 建造者模式主要包含四个角色: Product:产品角色. Builder:抽象建造者.它声明为创建一个Product对象的各个部件指定的抽象接口. ConcreteBuilder:具体 ...
- Code Signal_练习题_Array Replace
Given an array of integers, replace all the occurrences of elemToReplace with substitutionElem. Exam ...
- python学习之老男孩python全栈第九期_数据库day004 -- 作业
https://www.cnblogs.com/YD2018/p/9451809.html 11. 查询学过“001”并且也学过编号“002”课程的同学的学号.姓名 select student.si ...
- Fastify 系列教程二 (中间件、钩子函数和装饰器)
Fastify 系列教程: Fastify 系列教程一 (路由和日志) Fastify 系列教程二 (中间件.钩子函数和装饰器) Fastify 系列教程三 (验证.序列化和生命周期) Fastify ...
- nginx的MainLine version、Stable version、Legacy versions
Nginx的版本说明Mainline version:在线版本,正处于开发状态Stable version :稳定版本(一般下载使用)Legacy version :遗留版本,遗留的老的版本 Linu ...