//
// ViewController.m
// NeonLighting
//
// Created by 尹亚坤 on 13-11-14.
// Copyright (c) 2013年 蓝鸥科技. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
int flag = 0;
static inline CGRect rectWithScale(CGRect rect,CGFloat scale)//内联函数
{
rect.size.width += scale;
rect.size.height += scale;
return rect;
}
static inline CGRect rectWithScale1(CGRect rect,CGFloat scale)//内联函数
{
rect.size.width -= scale;
rect.size.height -= scale;
return rect;
}
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//添加一个灰色视图
UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
backView.backgroundColor = [UIColor grayColor];
backView.tag = 0;
[self.view addSubview:backView];
//添加一个白色视图
UIView * whiteView = [[UIView alloc]initWithFrame:CGRectMake(50, 60, 100, 100)];
whiteView.backgroundColor = [UIColor whiteColor];
whiteView.tag = 1;
[backView addSubview:whiteView];
//再添加一个白色视图
UIView * whiteView1 = [[UIView alloc]initWithFrame:CGRectMake(50, 270, 100, 100)];
whiteView1.backgroundColor = [UIColor whiteColor];
whiteView1.tag = 2;
[backView addSubview:whiteView1];
//添加开始按钮
UIButton * startButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
startButton.backgroundColor = [UIColor whiteColor];
startButton.frame = CGRectMake(10, 400, 100, 40);
startButton.tag = 3;
[startButton setTitle:@"start" forState:UIControlStateNormal];
[startButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
[backView addSubview:startButton];
//再添加一个交换按钮
UIButton * exchangeButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
exchangeButton.frame = CGRectMake(200, 400, 100, 40);
exchangeButton.tag = 4;
[exchangeButton setTitle:@"exchange" forState:UIControlStateNormal];
exchangeButton.backgroundColor = [UIColor whiteColor];
[exchangeButton addTarget:self action:@selector(didClickExchangeButton:) forControlEvents:UIControlEventTouchUpInside];
[backView addSubview:exchangeButton];
}
- (void) didClickExchangeButton:(UIButton *) sender
{
UIView *containerView = [self.view viewWithTag:0];
UIView * whiteView = [containerView viewWithTag:1];
UIView * whiteView1 = [containerView viewWithTag:2];
whiteView.tag = 2;
whiteView1.tag = 1;
}
NSTimer * timer1;
#pragma mark--------didClickButton
- (void)didClickButton:(UIButton *)sender
{
if ([sender.currentTitle isEqual:@"start"]) {
[sender setTitle:@"stop" forState:UIControlStateNormal];
timer1 = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(changeColor:) userInfo:nil repeats:YES];
}
else if ([sender.currentTitle isEqual:@"stop"])
{
[sender setTitle:@"start" forState:UIControlStateNormal];
[timer1 invalidate];
}
}
#pragma mark--------change color
- (void) changeColor:(UIButton *)sender
{
UIView * backView = [self.view viewWithTag:0];
UIView * whiteView = [backView viewWithTag:1];
NSArray * array = [whiteView subviews];
UIView * miniView = [self makeView];
whiteView.clipsToBounds = YES;
for (UIView * item in array) {
if (item.bounds.size.width > 100) {
[item removeFromSuperview];
}
item.bounds = rectWithScale(item.bounds, 5);
}
[whiteView addSubview:miniView];
UIView * whiteView1 = [backView viewWithTag:2];
NSArray * array1 = [whiteView1 subviews];
UIView * miniView1 = [self makeView1];
whiteView1.clipsToBounds = YES;
for (UIView * item in array1) {
if (item.bounds.size.width < 0) {
[item removeFromSuperview];
}
item.bounds = rectWithScale1(item.bounds, 5);
}
[whiteView1 insertSubview:miniView1 atIndex:0];
}
- (UIView *) makeView
{
UIView * colorView = [[UIView alloc]init];
CGFloat R = arc4random()%255 + 1;
CGFloat G = arc4random()%255 + 1;
CGFloat B = arc4random()%255 + 1;
// NSLog(@"%f,%f,%f",R,G,B);
colorView.backgroundColor = RGB(R, G, B);
UIView * backView = [self.view viewWithTag:0];
UIView * whiteView = [backView viewWithTag:1];
colorView.frame = CGRectMake(0, 0, 10, 10);
CGFloat X =CGRectGetMidX(whiteView.bounds);
CGFloat Y = CGRectGetMidY(whiteView.bounds);
colorView.frame = CGRectMake(X -5,Y -5, 10, 10);
return colorView;
}
- (UIView *) makeView1
{
UIView * colorView = [[UIView alloc]init];
CGFloat R = arc4random()%255;
CGFloat G = arc4random()%255;
CGFloat B = arc4random()%255;
// NSLog(@"%f,%f,%f",R,G,B);
colorView.backgroundColor = RGB(R, G, B);
colorView.frame = CGRectMake(0, 0, 100, 100);
return colorView;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end