#import “ViewController.h”
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface ViewController ()
@property (nonatomic, strong) NSMutableArray *userArray;
@property (nonatomic, strong) NSArray *allArray;
@property (nonatomic, strong) NSMutableArray *downArray;
@property (nonatomic, strong) UIScrollView *scrollView;
@end
@implementation ViewController
– (void)viewDidLoad {
[super viewDidLoad];
self.userArray = [NSMutableArray arrayWithArray:@[@”经典案例“, @”京东“, @”淘宝“, @”天猫“, @”阿里巴巴“, @”唯品会“, @”1号店“]];
self.allArray = @[@”经典案例“, @”京东“, @”淘宝“, @”天猫“, @”阿里巴巴“, @”唯品会“, @”1号店“, @”苏宁易购“, @”聚美优品“, @”我想静静“, @”静静是谁“, @”我是静静“];
self.downArray = [NSMutableArray arrayWithArray:@[@”苏宁易购“, @”聚美优品“, @”我想静静“, @”静静是谁“, @”我是静静“]];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
[self.view addSubview:scrollView];
self.scrollView = scrollView;
self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH,300+(self.downArray.count*60));
self.scrollView.backgroundColor = [UIColor clearColor];
[self setUpperItems];
[self setDownItems];
}
– (void)setUpperItems{
CGFloat aW = SCREEN_WIDTH/3–10;
CGFloat aH = aW/2.55;
int num = 3;
CGFloat marginX = (self.view.frame.size.width – aW * num) / (num + 1);
CGFloat marginY = 0;
for (int index = 0; index < self.userArray.count; index++)
{
UIButton * btn = [[UIButton alloc] init];
int col = index % num;
CGFloat aX = marginX + (aW + marginX) * col;
int row = index / num;
CGFloat aY = 20 + marginY + (aH + marginY) * row;
btn.frame = CGRectMake(aX, aY, aW, aH);
[btn setTitle:self.userArray[index] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.scrollView addSubview:btn];
[btn addTarget:self action:@selector(toDown:) forControlEvents:UIControlEventTouchUpInside];
}
}
– (void)setDownItems{
for (int index = 0; index < self.downArray.count; index++)
{
UIButton * btn = [[UIButton alloc] init];
btn.frame = CGRectMake(0, index*60+192, SCREEN_WIDTH, 60);
[btn setTitle:self.downArray[index] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.scrollView addSubview:btn];
[btn addTarget:self action:@selector(toUp:) forControlEvents:UIControlEventTouchUpInside];
}
}
– (void)toDown:(UIButton *)button{
// 动画效果
CGRect tempRect = CGRectMake(0, 200+self.downArray.count*60, SCREEN_WIDTH, 60);
[UIView animateWithDuration:0.3 animations:^{button.frame = tempRect;}];
// 生成上半部分需要的新的数组
NSString *str = [NSString stringWithString:button.titleLabel.text];
NSMutableArray *filteredArray = [[NSMutableArray alloc]initWithObjects:str, nil];
NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@”NOT (SELF IN %@)”,filteredArray];
self.userArray = [NSMutableArray arrayWithArray:[self.userArray filteredArrayUsingPredicate:filterPredicate]];
// 生成下半部分需要的新的数组
[self.downArray addObject:str];
// 延时执行
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 移除页面中所有的按钮
for(UIButton *btn in [self.scrollView subviews])
{
[btn removeFromSuperview];
}
// 重载页面中所有的按钮
[self setUpperItems];
[self setDownItems];
});
self.scrollVew.contentSize = CGSizeMake(SCREEN_WIDTH, 300+(self.downArray.count*60));
}
– (void)toUp:(UIButton *)button{
// 动画效果
CGFloat aW = SCREEN_WIDTH/3–10;
CGFloat aH = aW/2.55;
int num = 3;
CGFloat marginX = (self.view.frame.size.width – aW * num) / (num + 1);
CGFloat marginY = 0;
int col = (self.userArray.count) % num;
CGFloat aX = marginX + (aW + marginX) * col;
unsigned long row = (self.userArray.count)/num;
CGFloat aY = 20 + marginY + (aH + marginY) * row;
CGRect tempRect = CGRectMake(aX,aY,aW,aH);
[UIView animateWithDuration:0.3 animations:^{button.frame = tempRect;}];
// 生成上半部分需要的新的数组
NSString *str = [NSString stringWithString:button.titleLabel.text];
[self.userArray addObject:str];
// 生成下半部分需要的新的数组
NSMutableArray *filteredArray = [[NSMutableArray alloc]initWithObjects:str, nil];
NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@”NOT (SELF IN %@)”,filteredArray];
self.downArray = [NSMutableArray arrayWithArray:[self.downArray filteredArrayUsingPredicate:filterPredicate]];
// 延时执行
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 移除页面中所有的按钮
for(UIButton *btn in [self.scrollView subviews])
{
[btn removeFromSuperview];
}
// 重载页面中所有的按钮
[self setUpperItems];
[self setDownItems];
});
self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH,300+(self.downArray.count*60));
}
@end