设计可以多选的按钮ChooseManyButton
设计可以多选的按钮ChooseManyButton
效果:
源码:
ChooseManyButton.h 与 ChooseManyButton.m
- //
- // ChooseManyButton.h
- // ChooseOnlyButton
- //
- // Created by YouXianMing on 14/11/5.
- // Copyright (c) 2014年 YouXianMing. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @protocol ChooseManyButtonDelegate <NSObject>
- @optional
- - (void)chooseButtons:(NSArray *)buttons;
- @end
- @interface ChooseManyButton : UIButton
- /**
- * 代理
- */
- @property (nonatomic, assign) id<ChooseManyButtonDelegate> delegate;
- /**
- * 选择的标题(存储的是NSNumber的BOOL值,与给定的标题值一一对应)
- */
- @property (nonatomic, strong, readonly) NSMutableArray *chooseTitles;
- /**
- * 标题的数组
- */
- @property (nonatomic, strong) NSArray *titles;
- /**
- * 按钮离左侧的距离
- */
- @property (nonatomic, assign) CGFloat gapFromLeft;
- /**
- * 两个按钮之间的水平距离
- */
- @property (nonatomic, assign) CGFloat gapFromHorizontalButton;
- /**
- * 两个按钮之间的垂直间距
- */
- @property (nonatomic, assign) CGFloat gapFromVerticalButton;
- /**
- * 按钮高度
- */
- @property (nonatomic, assign) CGFloat buttonHeight;
- /**
- * 按钮标题字体
- */
- @property (nonatomic, strong) UIFont *buttonTitleFont;
- /**
- * 没有选中状态下的按钮的背景颜色以及按钮字体的颜色
- */
- @property (nonatomic, strong) UIColor *normalButtonBackgroundColor;
- @property (nonatomic, strong) UIColor *normalButtonTitleColor;
- /**
- * 选中状态下的按钮的背景颜色以及按钮字体的颜色
- */
- @property (nonatomic, strong) UIColor *selectedButtonBackgroundColor;
- @property (nonatomic, strong) UIColor *selectedButtonTitleColor;
- /**
- * 重设view的尺寸并且创建出新的按钮
- */
- - (void)resetSizeAndCreateButtons;
- /**
- * 重新计算frame
- *
- * @return frame值
- */
- - (CGRect)calculateFrame;
- @end
- //
- // ChooseManyButton.m
- // ChooseOnlyButton
- //
- // Created by YouXianMing on 14/11/5.
- // Copyright (c) 2014年 YouXianMing. All rights reserved.
- //
- #import "ChooseManyButton.h"
- typedef enum : NSUInteger {
- FLAG_BUTTON = 0x2244,
- } ENUM_FLAG;
- @interface ChooseManyButton ()
- /**
- * 标签数组
- */
- @property (nonatomic, strong) NSMutableArray *chooseTitles;
- @end
- @implementation ChooseManyButton
- - (void)resetSizeAndCreateButtons {
- // 没有元素则退出
- if (_titles.count == ) {
- return;
- }
- // 初始化标签数组(与标题一一对应)
- _chooseTitles = [NSMutableArray new];
- for (int i = ; i < _titles.count; i++) {
- [_chooseTitles addObject:[NSNumber numberWithBool:NO]];
- }
- // 没有设置左边距则默认值为5.f
- if (_gapFromLeft == ) {
- _gapFromLeft = .f;
- }
- // 没有设置水平按钮间距则默认值为5.f
- if (_gapFromHorizontalButton == ) {
- _gapFromHorizontalButton = .f;
- }
- // 没有设置垂直按钮间距则默认值为5.f
- if (_gapFromVerticalButton == ) {
- _gapFromVerticalButton = .f;
- }
- // 没有设置按钮高度则按钮默认高度为20.f
- if (_buttonHeight == ) {
- _buttonHeight = .f;
- }
- // 获取frame宽度
- CGFloat frameWidth = self.bounds.size.width;
- // 计算出按钮宽度
- CGFloat buttonWidth = (frameWidth - _gapFromLeft* - _gapFromHorizontalButton)/.f;
- // 动态创建出按钮
- for (int i = ; i < _titles.count; i++) {
- UIButton *button = [[UIButton alloc] initWithFrame:\
- CGRectMake(_gapFromLeft + (buttonWidth + _gapFromHorizontalButton)*(i%),
- (i/)*(_buttonHeight + _gapFromVerticalButton),
- buttonWidth,
- _buttonHeight)];
- button.tag = FLAG_BUTTON + i;
- // 设置按钮圆角
- button.layer.cornerRadius = _buttonHeight/.f;
- [button addTarget:self
- action:@selector(buttonsEvent:)
- forControlEvents:UIControlEventTouchUpInside];
- // 设置按钮标题 + 默认的标题颜色
- [button setTitle:_titles[i] forState:UIControlStateNormal];
- [self normalButtonStyle:button];
- // 设置字体
- if (_buttonTitleFont) {
- button.titleLabel.font = _buttonTitleFont;
- }
- [self addSubview:button];
- }
- // 重设自身view高度
- CGFloat selfViewHeight = _buttonHeight*((_titles.count - )/ + ) + _gapFromVerticalButton*((_titles.count - )/);
- CGRect rect = self.frame;
- rect.size.height = selfViewHeight;
- self.frame = rect;
- }
- - (CGRect)calculateFrame {
- // 没有元素则退出
- if (_titles.count == ) {
- return CGRectZero;
- }
- // 没有设置左边距则默认值为5.f
- if (_gapFromLeft == ) {
- _gapFromLeft = .f;
- }
- // 没有设置水平按钮间距则默认值为5.f
- if (_gapFromHorizontalButton == ) {
- _gapFromHorizontalButton = .f;
- }
- // 没有设置垂直按钮间距则默认值为5.f
- if (_gapFromVerticalButton == ) {
- _gapFromVerticalButton = .f;
- }
- // 没有设置按钮高度则按钮默认高度为20.f
- if (_buttonHeight == ) {
- _buttonHeight = .f;
- }
- // 根据控件的一些参数计算出高度
- CGFloat selfViewHeight = _buttonHeight*((_titles.count - )/ + ) + _gapFromVerticalButton*((_titles.count - )/);
- CGRect rect = self.frame;
- rect.size.height = selfViewHeight;
- // 返回控件
- return rect;
- }
- - (void)buttonsEvent:(UIButton *)button {
- // 获取到点击按钮的index值
- NSInteger index = button.tag - FLAG_BUTTON;
- // 根据值来确定设置样式
- if ([_chooseTitles[index] isEqual: @NO]) {
- _chooseTitles[index] = @YES;
- [self selectButtonStyle:button];
- } else {
- _chooseTitles[index] = @NO;
- [self normalButtonStyle:button];
- }
- // 通过代理获取点击了哪些按钮
- if (_delegate && [_delegate respondsToSelector:@selector(chooseButtons:)]) {
- [_delegate chooseButtons:_chooseTitles];
- }
- }
- #pragma mark - 私有方法
- /**
- * 普通按钮的样式
- *
- * @param button 要改变样式的按钮
- */
- - (void)normalButtonStyle:(UIButton *)button {
- if (_normalButtonTitleColor) {
- [button setTitleColor:_normalButtonTitleColor
- forState:UIControlStateNormal];
- } else {
- [button setTitleColor:[UIColor colorWithRed:0.000 green:0.361 blue:0.671 alpha:]
- forState:UIControlStateNormal];
- }
- if (_normalButtonBackgroundColor) {
- button.backgroundColor = _normalButtonBackgroundColor;
- } else {
- button.backgroundColor = [UIColor clearColor];
- }
- button.layer.borderColor = [UIColor colorWithRed:0.843 green:0.843 blue:0.843 alpha:].CGColor;
- button.layer.borderWidth = .f;
- }
- /**
- * 选中按钮时的样式
- *
- * @param button 要改变样式的按钮
- */
- - (void)selectButtonStyle:(UIButton *)button {
- if (_selectedButtonTitleColor) {
- [button setTitleColor:_selectedButtonTitleColor
- forState:UIControlStateNormal];
- } else {
- [button setTitleColor:[UIColor colorWithRed:0.973 green:0.984 blue:0.988 alpha:]
- forState:UIControlStateNormal];
- }
- if (_selectedButtonBackgroundColor) {
- button.backgroundColor = _selectedButtonBackgroundColor;
- } else {
- button.backgroundColor = [UIColor colorWithRed:0.055 green:0.365 blue:0.663 alpha:];
- }
- button.layer.borderColor = [UIColor colorWithRed:0.055 green:0.365 blue:0.663 alpha:].CGColor;
- button.layer.borderWidth = .f;
- }
- @end
使用时的代码:
- //
- // ViewController.m
- // ChooseOnlyButton
- //
- // Created by YouXianMing on 14/11/4.
- // Copyright (c) 2014年 YouXianMing. All rights reserved.
- //
- #import "ViewController.h"
- #import "ChooseManyButton.h"
- @interface ViewController ()<ChooseManyButtonDelegate>
- {
- ChooseManyButton *button;
- }
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
- label.textAlignment = NSTextAlignmentCenter;
- label.text = @"“近代中国睁眼看世界的第一人”的是___";
- label.textColor = [UIColor grayColor];
- [self.view addSubview:label];
- button = [[ChooseManyButton alloc] initWithFrame:CGRectMake(, , , )];
- button.buttonHeight = .f;
- button.gapFromLeft = .f;
- button.gapFromVerticalButton = .f;
- button.gapFromHorizontalButton = .f;
- button.buttonTitleFont = [UIFont systemFontOfSize:.f];
- button.titles = @[@"A. 梁启超",
- @"B. 龚自珍",
- @"C. 林则徐",
- @"D. 李鸿章"];
- button.delegate = self;
- [self.view addSubview:button];
- // 设置完所有参数后创建出控件
- [button resetSizeAndCreateButtons];
- }
- - (void)chooseButtons:(NSArray *)buttons {
- NSLog(@"%@", buttons);
- }
- @end
实时打印信息:
设计可以多选的按钮ChooseManyButton的更多相关文章
- 设计多选一按钮ChooseOnlyButton
设计多选一按钮ChooseOnlyButton 效果: 源码: ChooseOnlyButton.h 与 ChooseOnlyButton.m // // ChooseOnlyButton.h // ...
- 定制二选一按钮SwitchButton
定制二选一按钮SwitchButton 效果: 源码: SwitchButton.h 与 SwitchButton.m // // SwitchButton.h // KongJian // // C ...
- 界面设计中如何增强CTA按钮召唤力?
以下内容由Mockplus(摹客)团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 网页和软件应用之类数字产品的有效交互系统一般是由拥有各种任务和功能的小元素构成.而为创建更加 ...
- cocos2dx 3.x (单选,多选,复选checkBox按钮的实现) RadioButton
// // LandLordsMakeNewRoom.hpp // MalaGame39 // // Created by work on 2016/12/19. // // #ifndef ...
- JS实现简单的多选选项的全选反选按钮
1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <!-- 5 需求: 6 1.写三个按钮: ...
- js全选反选按钮实现
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 用DotNetBar设计的 Gradient Buttons 漂亮按钮
http://www.webdesignerwall.com/demo/css-buttons.html public class GradientButtons : DevComponents ...
- listview禁止双击一条之后选中复选框按钮的方法
this.listViewUsers.SelectedItems[0].Checked = !this.listViewUsers.SelectedItems[0].Checked;
- openerp学习笔记 tree视图增加复选处理按钮
wizard:用于确认或选择 wizard/sale_multi_action.py # -*- encoding: utf-8 -*-from openerp.osv import fields, ...
随机推荐
- Java 生成指定范围的随机数
/** * 生成[min, max]之间的随机整数 * * @param min 最小整数 * @param max 最大整数 * @return * @author jqlin */ private ...
- glide 解决 golang.org/x/net 等依赖包无法获取
知道glide有设置镜像功能,可以把某个依赖包的源地址切换为另一个地址,相当于切换到镜像地址,用于某些依赖包被墙的原因 之前碰到 golang.org/x/net,设置镜像: glide mirror ...
- Linux下安装了Xampp,但命令行使用不了MySQL
引用:http://www.2cto.com/database/201406/309521.html 刚安装好Xampp,使用localhost也能正常运行, phpMyAdimin也能正常登录 ...
- 深度剖析Dubbo源码
-----------------学习dubbo源码,能给你带来什么好处?----------- 1.提升SOA的微服务架构设计能力 通过读dubbo源码是一条非常不错的通往SOA架构设计之路,毕 ...
- Linux 文件流管理
1. 打开/关闭文件 1). 打开文件 / fopen 作用: 打开一个文件,将其与文件流联系起来,方便后续的操作 头文件: #include <stdio.h> 函数原型: FILE * ...
- Ionic APP 热更新 之 产品发布状态下的热更新搭建,去local-dev-addon插件
上一篇,我们介绍了在本地开发环境下的ionic项目热更新测试, 本文,我们将详细说明如何在去掉cordova-hot-code-push-local-dev-addon插件的情况下,实现热更新. 使用 ...
- c#基础学习(0703)之string.Format格式化日期
C# string.Format格式化日期 DateTime dt = ,,,,,,); string.Format("{0:y yy yyy yyyy}",dt); //17 1 ...
- 设计模式学习--面向对象的5条设计原则之接口隔离原则--ISP
一.ISP简介(ISP--Interface Segregation Principle): 使用多个专门的接口比使用单一的总接口要好.一个类对另外一个类的依赖性应当是建立在最小的接口上的.一个接口代 ...
- WPF进度条
ProgressBar控件与传统WinForm使用方法完全一样,我们只需关注: Minimum——最小值,默认为0: Maximum——最大值,默认为100. Value——当前值. 关键是它的控 ...
- day-01mysql数据库下载安装卸载及基本操作
MySQL5.5.40破解版地址(永久有效):链接:https://pan.baidu.com/s/1n-sODjoCdeSGP8bDGxl23Q 密码:qjjy 第2节 数据库的介绍 MySQL:开 ...