九宫格-选中变色-单选项
来源:互联网 发布:公安民警网络答题 编辑:程序博客网 时间:2024/06/11 01:07
//// ViewController.m// SelectedButtonDemo//// Created by 逍遥子 on 16/1/22.// Copyright © 2016年 QuantGroup. All rights reserved.//#import "ViewController.h"@interface ViewController ()@property (strong,nonatomic)UIButton*starButton;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self creatNineBox];}-(void)creatNineBox{ //设置九宫格按钮显示的title数组 NSArray *titileArray = @[@"无知",@"风云变幻",@"施耐庵"]; // 总列数 int totalColumns = 3; // 每一格的尺寸 CGFloat cellW = 100; CGFloat cellH = 40; // 间隙 CGFloat margin =(self.view.frame.size.width - totalColumns * cellW) / (totalColumns + 1); // 根据格子个数创建对应的框框 for(int index = 0; index< titileArray.count; index++) { UIButton *boxBtn = [UIButton buttonWithType:UIButtonTypeCustom]; //设置tag boxBtn.tag=index+666; boxBtn.backgroundColor = [UIColor whiteColor]; boxBtn.layer.borderColor=[UIColor grayColor].CGColor; boxBtn.layer.borderWidth=1; boxBtn.layer.cornerRadius=3; [boxBtn setTitle:[titileArray objectAtIndex:index] forState:UIControlStateNormal]; [boxBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; boxBtn.titleLabel.font=[UIFont systemFontOfSize:12]; // 计算行号 和 列号 int row = index / totalColumns; int col = index % totalColumns; //根据行号和列号来确定 子控件的坐标 CGFloat cellX = margin + col * (cellW + margin); CGFloat cellY = row * (cellH + margin);// 下移100 boxBtn.frame = CGRectMake(cellX, cellY +200, cellW, cellH); [boxBtn addTarget:self action:@selector(selectedBtnPress:) forControlEvents:UIControlEventTouchUpInside]; // 添加到view 中 [self.view addSubview:boxBtn]; }}- (void)selectedBtnPress:(UIButton*)sender{ //遍历一下、找到我们的按钮 for (id obj in self.view.subviews) { if ([obj isKindOfClass:[UIButton class]]) { UIButton* theButton = (UIButton*)obj; //为我们选中和不被选中的按钮设置不同的颜色 if (theButton.tag ==sender.tag) { theButton.backgroundColor =[UIColor orangeColor]; theButton.layer.borderColor=[UIColor orangeColor].CGColor; [theButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; }else{ theButton.backgroundColor =[UIColor whiteColor]; theButton.layer.borderColor=[UIColor grayColor].CGColor; [theButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; } } }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
效果
0 0
- 九宫格-选中变色-单选项
- jQuery 各行变色,选中变色
- GridView 选中行变色
- js 选中变色
- GridView选中行变色
- Android ListView选中变色
- ListView单行选中变色
- GridView选中行变色
- 选中行变色
- 变色选项卡
- setSingleChoiceItems方法-使单选项列表框默认选中前次选中的值
- 隔行变色以及选中高亮/变色
- 园角INPUT,INPUT选中变色?!
- 文字选中后背景变色
- GridView选中变色不刷新
- GridView选中、经过行变色
- radio文本选中行变色
- DataGridView 选中一行不变色
- 博客已迁移新地址
- java实现MD5加密
- phpStorm使用技巧总结
- BZOJ 1127: [POI2008]KUP|单调队列|思路题
- MySQL C API mysql_free_result() 详解
- 九宫格-选中变色-单选项
- SVN使用小结
- 过滤器及异步线程使用试验
- Android 获取设备和系统信息
- JavaSE003_String类总结之内存分析
- FFMPEG研究: FFmpeg的Android平台移植编译
- 友善开发板移植uboot-2010.03
- ant安装配置使用介绍及eclipse中使用
- windows 64 intel 安装Numpy 报错Microsoft Visual C++ 9.0 is required Unable to find