首页 > 移动平台 > 详细

iOS 霓虹灯效果

时间:2015-08-16 19:45:18      阅读:248      评论:0      收藏:0      [点我收藏+]

总体思路如下:

     利用for 循环创建N个view,并将他们添加到可变数组中,利用NSTimer,实现循环变色的效果(changeColor:),变色的思路:将第i个view 的背景色 和第i + 1 个view 的背景色交换,换完一轮即可,(NSTimer 会自动实现循环)

这里是在试图控制器中写的,所以 把view 添加到self.view里, self.view.subviews(是一个数组(里边是各个VIEW))

- (void)viewDidLoad {
    [super viewDidLoad];

for (int i = 0; i < 6; i++) {
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(i * 20, i * 40, self.view.frame.size.width - i * 40, self.view.frame.size.height - i * 80)];//创建VIEW  
        view.backgroundColor = [UIColor colorWithRed: arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];//背景颜色随机赋值
        [self.view addSubview:view];//将其添加到(数组)
        [view release];
    }

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:.8 target:self selector:@selector(changeColor:) userInfo:self.view.subviews repeats:YES];//一直循环(把self.view.subviews 传到(changeColor:)里)
    [timer fire];

 

- (void)changeColor:(NSTimer *)sender
{
    NSMutableArray * ary = [NSMutableArray arrayWithArray:sender.userInfo];
    UIView * v = [[UIView alloc] init];
    for (int i = 0; i < ary.count - 1; i++) {//将第i个view 的背景色 和第i + 1 个view 的背景色交换
        UIView * v1 = [ary objectAtIndex:i];
        UIView * v2 = [ary objectAtIndex:i + 1];
        v.backgroundColor = v1.backgroundColor;
        v1.backgroundColor = v2.backgroundColor;
        v2.backgroundColor  = v.backgroundColor;
    }
}

iOS 霓虹灯效果

原文:http://www.cnblogs.com/lzcdbk/p/4734795.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!