学了ios的基础控件的一小部分,做了一个小作业。要求排列显示图片的表情,点击不同的按钮,分别按不同的列数排列,而且点击加号会增加一个表情。
我的实现是存代码搭建的界面,所以代码只添加在ZYHViewController.m文件中,其他文件没有修改,除了添加素材。
运行结果:
分享下代码(ZYHViewController.m):
//
// ZYHViewController.m
// 0521图片排列
//
// Created by zhouxf on 14-5-21.
// Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//
#import "ZYHViewController.h"
@interface ZYHViewController ()
/** 存放表情数组 */
@property (nonatomic, strong) NSMutableArray *picMA;
// 4个排列按钮:分别按2、3、4、5列排列
@property (nonatomic, strong) UIButton *sortBtnby2;
@property (nonatomic, strong) UIButton *sortBtnby3;
@property (nonatomic, strong) UIButton *sortBtnby4;
@property (nonatomic, strong) UIButton *sortBtnby5;
/** 增加表情按钮 */
@property (nonatomic, strong) UIButton *addBtn;
@end
@implementation ZYHViewController
- (NSArray *)picMA
{
if (!_picMA) {
// 初始化数组
_picMA = [[NSMutableArray alloc] init];
for (int i = 10; i < 19; i++) {
// 创建图片视图
UIImageView *picView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%03d", i]]];
// 加入大视图
[self.view addSubview:picView];
// 加入数组
[_picMA addObject:picView];
}
}
return _picMA;
}
- (UIButton *)sortBtnby2
{
if (!_sortBtnby2) {
// 通过tag、位置和标题初始化按钮
_sortBtnby2 = [self buttonInitWithTag:2 andRct:CGRectMake(0, 15, self.view.frame.size.width / 4, 40) andTitle:@"2列"];
}
return _sortBtnby2;
}
- (UIButton *)sortBtnby3
{
if (!_sortBtnby3) {
// 通过tag、位置和标题初始化按钮
_sortBtnby3 = [self buttonInitWithTag:3 andRct:CGRectMake(self.sortBtnby2.frame.origin.x + self.sortBtnby2.frame.size.width, 15, self.view.frame.size.width / 4, 40) andTitle:@"3列"];
}
return _sortBtnby3;
}
- (UIButton *)sortBtnby4
{
if (!_sortBtnby4) {
// 通过tag、位置和标题初始化按钮
_sortBtnby4 = [self buttonInitWithTag:4 andRct:CGRectMake(self.sortBtnby3.frame.origin.x + self.sortBtnby2.frame.size.width, 15, self.view.frame.size.width / 4, 40) andTitle:@"4列"];
}
return _sortBtnby4;
}
- (UIButton *)sortBtnby5
{
if (!_sortBtnby5) {
// 通过tag、位置和标题初始化按钮
_sortBtnby5 = [self buttonInitWithTag:5 andRct:CGRectMake(self.sortBtnby4.frame.origin.x + self.sortBtnby2.frame.size.width, 15, self.view.frame.size.width / 4, 40) andTitle:@"5列"];
}
return _sortBtnby5;
}
/** 通过tag、位置和标题初始化按钮 */
- (UIButton *)buttonInitWithTag:(int)tag andRct:(CGRect)rect andTitle:(NSString *)title
{
// 通过位置初始化按钮
UIButton *button = [[UIButton alloc] initWithFrame:rect];
// 设置文字字体
button.titleLabel.font = [UIFont boldSystemFontOfSize:13];
// 设置tag
button.tag = tag;
// 设置普通状态下的文字
[button setTitle:title forState:UIControlStateNormal];
// 设置普通状态下的文字颜色
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
// 设置失效状态下的文字颜色
[button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
// 加入视图
[self.view addSubview:button];
// 增加监听动作
[button addTarget:self action:@selector(sortAction:) forControlEvents:UIControlEventTouchUpInside];
return button;
}
/** 增加一个图片表情 */
- (void)addPic:(UIButton *)button
{
// 计算当前增加哪个表情
int count = self.picMA.count % 9 + 10;
UIImageView *picView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%03d", count]]];
// 加入视图
[self.view addSubview:picView];
// 加入数组
[self.picMA addObject:picView];
// 排列图片表情
[self sortAction:button];
}
- (UIButton *)addBtn
{
if (!_addBtn) {
// 设置加号按钮属性
_addBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
[_addBtn setImage:[UIImage imageNamed:@"plus_normal"] forState:UIControlStateNormal];
[_addBtn setImage:[UIImage imageNamed:@"plus_highlighted"] forState:UIControlStateHighlighted];
// 加入视图
[self.view addSubview:_addBtn];
// 增加监听动作
[_addBtn addTarget:self action:@selector(addPic:) forControlEvents:UIControlEventTouchUpInside];
}
return _addBtn;
}
/** 排列图片表情 */
- (void)sortAction:(UIButton *)button
{
int no = button.tag;
// 设置按钮是否失效
[self.sortBtnby2 setEnabled:(no != 2)];
[self.sortBtnby3 setEnabled:(no != 3)];
[self.sortBtnby4 setEnabled:(no != 4)];
[self.sortBtnby5 setEnabled:(no != 5)];
// 保存当前的列数
self.addBtn.tag = no;
// 图片表情的个数
int count = self.picMA.count;
// 图片表情的宽度和高度
int picWH = 30;
// 图片与图片之间的间距
int rowSpace = (self.view.frame.size.width - picWH * no) / (no + 1);
int i;
// 后面的代码加入动画
[UIView beginAnimations:nil context:nil];
// 设置动画时长
[UIView setAnimationDuration:2];
// 计算图片表情的位置
for (i = 0; i < count; i++) {
[self.picMA[i] setFrame:CGRectMake(rowSpace + i % no * (rowSpace + picWH), self.sortBtnby2.frame.origin.x + self.sortBtnby2.frame.size.height + i / no * picWH, picWH, picWH)];
}
//计算加号按钮的位置
self.addBtn.frame = CGRectMake(rowSpace + i % no * (rowSpace + picWH), self.sortBtnby2.frame.origin.x + self.sortBtnby2.frame.size.height + i / no * picWH, picWH, picWH);
// 提交动画
[UIView commitAnimations];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 排列图片表情
[self sortAction:self.sortBtnby2];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end