霓虹灯1

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    //timer变量在实现方法中声明就只在.m文件中可以使用,这样即使其他文件引入了它的.h文件,timer也是私有的,没有修改的危险
    NSTimer *timer;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    //创建灰色容器视图...
    UIView *containerView = [[UIView alloc] initWithFrame:self.view.bounds];
    containerView.tag = 1001;
    containerView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:containerView];
    
    //创建白色窗口视图
    UIView *whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    whiteView.tag = 1002;
    whiteView.center = CGPointMake(CGRectGetMidX(containerView.bounds), 240);
    whiteView.backgroundColor = [UIColor whiteColor];
    [containerView addSubview:whiteView];
    
    //创建点击按钮
    UIButton *controlButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    controlButton.frame = CGRectMake(0, 0, 100, 30);
    controlButton.center = CGPointMake(CGRectGetMidX(containerView.bounds), 400);
    [controlButton setTitle:@"Start" forState:UIControlStateNormal];
    [controlButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [controlButton addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:controlButton];
}
- (void)didClickButton:(UIButton *)sender
{
    //当按钮名为Start时,点击按钮名变为Stop,图层开始放大,计时器开启,每隔0.2s重复一次timerMethods方法
    if ([sender.titleLabel.text isEqualToString:@"Start"]) {
        timer = [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(timerMethods) userInfo:nil repeats:YES];
        [sender setTitle:@"Stop" forState:UIControlStateNormal];
        
        return;
    }
    //点击Stop计时器停止,按钮名重新变为Start
    [timer invalidate];
    [sender setTitle:@"Start" forState:UIControlStateNormal];
}

- (void)timerMethods
{
    //viewDidLoad方法中的指针变量containerView和whiteView是在栈内存中的,出了这个方法就被销毁了,通过之前在viewDidLoad方法中设置的对象的tag值,重新定义一个指针,就可以通过tag值再次找到相应的对象。
    UIView *containerView = [self.view viewWithTag:1001];
    UIView *whiteView = [containerView viewWithTag:1002];
    //clipToBounds 剪切子视图到本视图坐标系统内,默认是NO
    whiteView.clipsToBounds = YES;
    //遍历所有的子视图..变大
    for (UIView *itemView in whiteView.subviews) {
        CGRect bounds = itemView.bounds;
        bounds.size.width *= 1.07;
        bounds.size.height *= 1.07;
        itemView.bounds = bounds;
        
        //如果itemView的宽度大于或者等于白色窗口视图的宽度时
        if (CGRectGetWidth(itemView.bounds) >= CGRectGetWidth(whiteView.bounds))
        {
            //将子视图,从父视图中移除....
            [itemView removeFromSuperview];
        }
    }
    
    //创建新的小视图
    UIView *miniView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];
    miniView.center = CGPointMake(CGRectGetMidX(whiteView.bounds), CGRectGetMidY(whiteView.bounds));
    miniView.backgroundColor = [UIColor colorWithRed:arc4random() % 1000 / 1000.0f green:arc4random() % 1000 / 1000.0f blue:arc4random() % 1000 / 1000.0f alpha:1.0f];
    [whiteView addSubview:miniView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值