码迷,mamicode.com
首页 > 其他好文 > 详细

霓虹灯的制作(正向,反向,以及停止)

时间:2014-08-23 11:25:30      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:uiview   ui   uibutton   

//先创建一个霓虹灯视图
    NSArray *arr = [NSArray arrayWithObjects:[UIColor purpleColor],[UIColor cyanColor],[UIColor blueColor],[UIColor greenColor],[UIColor yellowColor],[UIColor orangeColor],[UIColor redColor], nil];
    for (int i = 0; i < 7; i++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20 + i * 20 , 120 + i * 20, 280 - i * 20 * 2, 280 - i * 20 * 2)];
        view.backgroundColor = arr[i];
        view.tag = 100 + i;
        [_containerView addSubview:view];
        [view release];
    }
//创建按钮控制霓虹灯停止
    UIButton *bun = [UIButton buttonWithType:UIButtonTypeSystem];
    bun.frame = CGRectMake(50, 420, 220, 40);
    bun.layer.cornerRadius = 5;
    [bun setTitle:@"停止" forState:UIControlStateNormal];
    [bun addTarget:self action:@selector(bun:) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:bun];

//创建让霓虹灯反向运行的按钮
    UIButton *stop = [UIButton buttonWithType:UIButtonTypeSystem];
    stop.frame = CGRectMake(50, 480, 220, 40);
    stop.layer.cornerRadius = 5;
    [stop setTitle:@"反向" forState:UIControlStateNormal];
    [stop addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [_containerView addSubview:stop];

//设置时间间隔
     _time =  [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(test) userInfo:nil repeats:YES];

//停止事件
- (void)bun:(UIButton *)btn
{
    [_time invalidate];
}

//正向运行点击事件
- (void)test
{
   UIView *view = [[UIView alloc] init];
    view.backgroundColor = [_containerView viewWithTag:100].backgroundColor;
    for (int i = 100; i < 107; i++) {
        [_containerView viewWithTag:i].backgroundColor = [_containerView viewWithTag:i + 1].backgroundColor;
    }
     [_containerView viewWithTag:106].backgroundColor = view.backgroundColor;
    [view release];
}

//反向运行点击事件
- (void)click
{
     _time =  [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(clicks) userInfo:nil repeats:YES];
}

- (void)clicks
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [_containerView viewWithTag:106].backgroundColor;
    for (int i = 106; i > 99; i--) {
        [_containerView viewWithTag:i].backgroundColor = [_containerView viewWithTag:i - 1].backgroundColor;
    }
    [_containerView viewWithTag:100].backgroundColor = view.backgroundColor;
    [view release];
}


通过以上步骤,就基本完成了一个可以正向,反向,以及停止的霓虹灯制作.

bubuko.com,布布扣

file:///Users/lanouhn/Desktop/1.gif

霓虹灯的制作(正向,反向,以及停止)

标签:uiview   ui   uibutton   

原文地址:http://blog.csdn.net/w_sx_/article/details/38776293

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!