#import "ViewController.h"
@interface ViewController ()
@property UISegmentedControl *segment;
@property NSMutableArray *ary;
@property UIButton *addBtn;
@property UIButton *delectBtn;
@property NSInteger h;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//设置segment的数量以及位置
NSMutableArray *setAry = [NSMutableArray arrayWithObjects:@"两列",@"三列",@"四列",@"五列", nil];
_segment = [[UISegmentedControl alloc] initWithItems:setAry];
_segment.frame = CGRectMake(20, 60, 280, 30);
_segment.tintColor = [UIColor redColor];
[self.view addSubview:_segment];
//为数组导入表情图标
_ary = [[NSMutableArray alloc] init];
for (int i = 0 ; i < 9; i++) {
UIButton *faceBtn = [[UIButton alloc] init];
faceBtn.frame = CGRectMake(i*20, 568, 30, 30);
[faceBtn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"face%i.png",i+1]] forState:UIControlStateNormal];
[faceBtn setTag:i];
[faceBtn addTarget:self action:@selector(delect:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:faceBtn];
[_ary addObject:faceBtn];
}
//数组增加按钮的添加
UIButton *addButton = [[UIButton alloc] init];
addButton.frame = CGRectMake(20, 568, 30, 30);
[addButton setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"add.png"]] forState:UIControlStateNormal];
[self.view addSubview:addButton];
[_ary addObject:addButton];
[_segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
[addButton addTarget:self action:@selector(addShow:) forControlEvents:UIControlEventTouchUpInside];
//调用背景颜色更换的函数
[NSTimer scheduledTimerWithTimeInterval:9.8 target:self selector:@selector(changeColor) userInfo:nil repeats:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark segment列数的选择
- (IBAction)segmentAction:(UISegmentedControl *)sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
_h =sender.selectedSegmentIndex+2;
[self paiXu:_h];
[UIView areAnimationsEnabled];
}
#pragma mark 增加按钮
- (IBAction)addShow:(UIButton *)sender
{
UIButton *faceBtn = [[UIButton alloc] init];
faceBtn.frame = CGRectMake(0, 0, 30, 30);
[faceBtn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"face%i.png",arc4random()%9+1]] forState:UIControlStateNormal];
[faceBtn setTag:_ary.count-1];
[faceBtn addTarget:self action:@selector(delect:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:faceBtn];
[_ary insertObject:faceBtn atIndex:_ary.count-1];
[self paiXu:_h];
}
#pragma mark 删除表情
- (IBAction)delect:(UIButton *)sender
{
[_ary removeObjectAtIndex:sender.tag];
[sender removeFromSuperview];
for (int i = sender.tag; i<_ary.count-1; i++) {
[_ary[i] setTag:i];
}
[self paiXu:_h];
}
#pragma mark 排序
- (void)paiXu:(int)h
{
//根据表情坐标XX YY之间的间隙
double jianxiXX = (double)(self.view.frame.size.width - _h*30)/(_h+1);
double jianxiYY = 20;
for (int i=0; i<_ary.count-1; i++) {
UIButton *face = (UIButton*)_ary[i+1];
CGRect frameF = face.frame;
//x坐标 (和列数有关系)
int nowLie = i%_h; //列:0.1.2...
frameF.origin.x = (nowLie+1)*jianxiXX+nowLie*30;
//y坐标 (和行数有关系)
int nowHang = i/_h; //行:0.1.2...
frameF.origin.y = 100+nowHang*(jianxiYY+30);
face.frame = frameF;
}
//;根据行.列数量判断
// int k = 100 ;
// int g = 0 ;
// int r = 1 ;
// UIButton *btn ;
// CGRect fra ;
// for (int i=0; i<(_ary.count/_h+1); i++)
// {
// for (int j=0; j<_h; j++)
// {
// if (g==_ary.count)
// break;
// btn=(UIButton *)_ary[g];
// fra=btn.frame;
// fra.origin.x=320/(_h*2)*r;
// fra.origin.y=k;
// btn.frame=fra;
// g++;
// r=r+2;
// }
// k=k+45;
// r=1;
// }
}
#pragma mark 设定时间随机替换背景色
- (void)changeColor
{
[self.view setBackgroundColor:[[UIColor alloc] initWithRed:(double)(arc4random()%256)/256 green:(double)(arc4random()%256)/256 blue:(double)(arc4random()%256)/256 alpha:0.8]];
}
@end
控制列来进行表情排序
最新推荐文章于 2024-10-25 16:51:20 发布