实现霓虹灯闪烁效果(UI)




//延展添加对象

@interface AppDelegate ()
{
    UIView *_containerView;
    NSTimer *_timer;
}
@end


//- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中函数体//


    _containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    _containerView.backgroundColor = [UIColor clearColor];
    [self.window addSubview:_containerView];
    [_containerView release];
    
    NSArray *array = [NSArray arrayWithObjects:[UIColor redColor], [UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor blueColor], [UIColor cyanColor], [UIColor purpleColor], nil];
    
    for (int i = 0; i < 7; i++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20 + i * 20, 120 + i * 20, 280 - i * 40, 280 - i * 40)];
        view.backgroundColor = array[i];
        view.tag = i + 200;
        NSLog(@"%ld", (long)view.tag);
        [_containerView addSubview:view];
        [view release];
    }
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem
                         ];
    button1.frame = CGRectMake(40, 450, 70, 40);
    button1.backgroundColor = [UIColor cyanColor];
    [button1 setTitle:@"由内而外" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(changeFromInToOut) forControlEvents:UIControlEventTouchUpInside];
    button1.layer.cornerRadius = 7;
    [_containerView addSubview:button1];
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem
                         ];
    button2.frame = CGRectMake(125, 450, 70, 40);
    button2.backgroundColor = [UIColor cyanColor];
    [button2 setTitle:@"停止" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(stop) forControlEvents:UIControlEventTouchUpInside];
    button2.layer.cornerRadius = 7;
    [_containerView addSubview:button2];
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem
                         ];
    button3.frame = CGRectMake(210, 450, 70, 40);
    button3.backgroundColor = [UIColor cyanColor];
    [button3 setTitle:@"由外而内" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(changeFromOutToIn) forControlEvents:UIControlEventTouchUpInside];
    button3.layer.cornerRadius = 7;
    [_containerView addSubview:button3];

//设置计时器

- (void)changeFromInToOut
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(changeInToOut) userInfo:nil repeats:YES];
}


- (void)changeFromOutToIn
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(changeOutToIn) userInfo:nil repeats:YES];
}


// 暂停计时器
- (void)stop
{
    [_timer invalidate];
}


// 由内而外循环交换颜色
- (void)changeInToOut
{
    UIView *temp = [[UIView alloc] init];
    int i = 200;
    temp.backgroundColor = [_containerView viewWithTag:200].backgroundColor;
    for (i = 200; i < 207; i++) {
        [_containerView viewWithTag:i].backgroundColor = [_containerView viewWithTag: i + 1].backgroundColor;
    }
    [_containerView viewWithTag:206].backgroundColor =temp.backgroundColor;
    [temp release];
}


// 由外而内循环交换颜色
- (void)changeOutToIn
{
    UIView *temp = [[UIView alloc] init];
    int i = 206;
    temp.backgroundColor = [_containerView viewWithTag:206].backgroundColor;
    for (i = 206; i >= 0; i--) {
        [_containerView viewWithTag:i].backgroundColor = [_containerView viewWithTag: i - 1].backgroundColor;
    }
    [_containerView viewWithTag:200].backgroundColor =temp.backgroundColor;
    [temp release];
}







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值