设计多选一按钮ChooseOnlyButton
设计多选一按钮ChooseOnlyButton

效果:

源码:
ChooseOnlyButton.h 与 ChooseOnlyButton.m
//
// ChooseOnlyButton.h
// ChooseOnlyButton
//
// Created by YouXianMing on 14/11/4.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import <UIKit/UIKit.h>
@class ChooseOnlyButton; @protocol ChooseOnlyButtonDelegate <NSObject>
@optional
- (void)chooseButtonTitle:(NSString *)title;
@end @interface ChooseOnlyButton : UIView /**
* 代理
*/
@property (nonatomic, assign) id<ChooseOnlyButtonDelegate> delegate; /**
* 选取的按钮的标题(只读)
*/
@property (nonatomic, strong, readonly) NSString *selectedTitle; /**
* 标题的数组
*/
@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
//
// ChooseOnlyButton.m
// ChooseOnlyButton
//
// Created by YouXianMing on 14/11/4.
// Copyright (c) 2014年 YouXianMing. All rights reserved.
// #import "ChooseOnlyButton.h" @implementation ChooseOnlyButton - (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) { }
return self;
} - (void)resetSizeAndCreateButtons { // 没有元素则退出
if (_titles.count == ) {
return;
} // 没有设置左边距则默认值为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.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 { [[self subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[UIButton class]]) {
if ([button isEqual:obj]) {
// 选中按钮的样式
[self selectButtonStyle:obj]; UIButton *button = (UIButton *)obj; // 获取到选取的按钮标题
_selectedTitle = button.titleLabel.text; // 代理
if (_delegate && [_delegate respondsToSelector:@selector(chooseButtonTitle:)]) { [_delegate chooseButtonTitle:button.titleLabel.text];
}
} else {
[self normalButtonStyle:obj];
}
}
}];
} #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 "ChooseOnlyButton.h" @interface ViewController ()<ChooseOnlyButtonDelegate> {
ChooseOnlyButton *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 = [[ChooseOnlyButton alloc] initWithFrame:CGRectMake(, , , )];
button.buttonHeight = .f;
button.gapFromLeft = .f;
button.gapFromVerticalButton = .f;
button.gapFromHorizontalButton = .f;
button.buttonTitleFont = [UIFont systemFontOfSize:.f];
button.titles = @[@"A. 蓝翔",
@"B. blueShit",
@"C. YouXianMing",
@"D. 不知道"];
button.delegate = self; [self.view addSubview:button]; // 设置完所有参数后创建出控件
[button resetSizeAndCreateButtons];
} #pragma mark - 代理
- (void)chooseButtonTitle:(NSString *)title {
NSLog(@"%@", title);
NSLog(@"%@", button.selectedTitle);
} @end
以下是需要注意的地方:
超高的可定制性

设计多选一按钮ChooseOnlyButton的更多相关文章
- 设计可以多选的按钮ChooseManyButton
设计可以多选的按钮ChooseManyButton 效果: 源码: ChooseManyButton.h 与 ChooseManyButton.m // // ChooseManyButton.h / ...
- 设计多选按钮ListChooseView
设计多选按钮ListChooseView 答应某位女屌丝而写的控件,效果还不错,开源给大家^_^! 效果图: 源码: // // ListChooseView.h // ScrollChooseBut ...
- js动态获取子复选项并设计全选及提交
在做项目的时候,会遇到根据父选项,动态的获取子选项,并列出多个复选框,提交时,把选中的合并成一个字符提交后台 本章将讲述如何通过js控制实现该操作: 1:设计父类别为radio,为每一个radio都加 ...
- 定制二选一按钮SwitchButton
定制二选一按钮SwitchButton 效果: 源码: SwitchButton.h 与 SwitchButton.m // // SwitchButton.h // KongJian // // C ...
- .NET开源工作流RoadFlow-表单设计-复选按钮组
复选按钮组的设置与单选按钮组的设置相同,只是表现形式为:<input type="checkbox"/>
- cocos2dx 3.x (单选,多选,复选checkBox按钮的实现) RadioButton
// // LandLordsMakeNewRoom.hpp // MalaGame39 // // Created by work on 2016/12/19. // // #ifndef ...
- xmlplus 组件设计系列之二 - 按钮
除了图标以外,按钮也许是最简单的组件了,现在来看看如何定义按钮组件. 使用原生按钮组件 在 xmlplus 中,HTML 元素也以组件的方式存在.所以,你可以直接通过使用 button 标签或者 in ...
- 聊聊Zookeeper应用场景、架构设计、选主机制
Zookeeper作为一个分布式协调系统提供了一项基本服务:分布式锁服务,分布式锁是分布式协调技术实现的核心内容.像配置管理.任务分发.组服务.分布式消息队列.分布式通知/协调等,这些应用实际上都是基 ...
- UI设计, 弹出对话框 设计(区分强调按钮和普通按钮,给用户一个 正向建议的强调按钮)
在UI设计时,经常会需要 设计 弹出对话框,以下是个样式设计: 0.标准对话框 说明 Title space : 标题区 contents space : 内容区 function space: 功能 ...
随机推荐
- Linux运维中遇到的常见问题
1.CentOS启动tomcat出现乱码的解决方案1.打开tomcat下的server.xml配置文件,在connect标签中添加编码属性:URIEncoding="UTF-8"2 ...
- @Override 注解compiler1.5和compiler1.6不同
说到注解问题,@interface 来定义注解类 这个注解出现是在jdk1.5版本出现. jdk1.5只支持@override对类的继承中方法重写的使用,不支持接口实现中方法重写的使用(这种情况下会报 ...
- java中的复制数组arraycopy()
System.arraycopy();//静态方法,在System类中定义,注意copy首字母是小写的 例子: int[] a = {1,2,3,4,5}; int[] b = {9,8,7,6}; ...
- BI实战派:医疗BI项目落地方案
任何BI项目面临的两大难题是项目价值和基础数据,BI项目应该给企业带来管理优化.业绩增长.医院面临的两大难题,一:绩效管理(奖金分配):二:医患关系:在医院开始自负盈亏时日常基本运营管理显得非常重要. ...
- redis实战笔记(6)-第6章 使用 Redis构建应用程序组件
本章主要内容 1.构建两个前缀匹配自 动补全程序 2.通过构建分布式锁来提高性能 3.通过开发计数信号量来控制并发 4.构建两个不同用途的任务队列 5.通过消息拉取系统来实现延迟消息传递 6.学习 ...
- 百度前端技术学院-task1.8源代码
主要是不采用bootstrap实现网格. 遇到的困难及注意点如下: 1.[class*='col-'],这个是选择col-开头的类,第一次用,以前也只是看到过: 2.媒体查询,总觉得容易理解错误.@m ...
- Shell脚本编写3---Shell 传递参数
我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… 执行脚本,查看输出结果: 另外 ...
- 使用phpQuery轻松采集网页内容
phpQuery是一个基于PHP的服务端开源项目,它可以让PHP开发人员轻松处理DOM文档内容,比如获取某新闻网站的头条信息.更有意思的是,它采用了jQuery的思想,你可以像使用jQuery一样处理 ...
- JavaScript学习笔记系列1:JavaScript的是什么?
一.JS是什么? JavaScript作为Netscape Navigator浏览器的一部分首次出现在1996年.它最初的设计目标是改善网页的用户体验. 作者:Brendan Eich 期初JavaS ...
- 三:vim常用快捷键
窗口移动操作: j或者Ctrl+e(就是Ctrl+e):向下细微滚动窗口. k或者Ctrl+y:向上细微滚动窗口. h:向左细微滚动窗口. l:向右细微滚动窗口. gg:跳转到页面的顶部. G(就是s ...