霓虹灯
基于OC实现的霓虹灯效果,看着有点刺眼。。。不过很好玩。
效果图:
你需要做的东西,就是用Xcode新建一个iOS工程,然后将下面代码敲上就可以实现。
代码如下:
UIView *yellow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 600)];
yellow.backgroundColor = [UIColor yellowColor];
yellow.center = CGPointMake((self.window.frame.size.width) / 2, (self.window.frame.size.height) / 2);
yellow.tag = 1;
[self.window addSubview:yellow];
[yellow release];
UIView *red = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 500)];
red.backgroundColor = [UIColor redColor];
red.center = CGPointMake((self.window.frame.size.width) / 2, (self.window.frame.size.height) / 2);
red.tag = 2;
[self.window addSubview:red];
[red release];
UIView *blue = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 225, 400)];
blue.backgroundColor = [UIColor blueColor];
blue.center = CGPointMake((self.window.frame.size.width) / 2, (self.window.frame.size.height) / 2);
blue.tag = 3;
[self.window addSubview:blue];
[blue release];
UIView *green = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 300)];
green.backgroundColor = [UIColor greenColor];
green.center = CGPointMake((self.window.frame.size.width) / 2, (self.window.frame.size.height) / 2);
green.tag = 4;
[self.window addSubview:green];
[green release];
UIView *gray = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 75, 200)];
gray.backgroundColor = [UIColor grayColor];
gray.center = CGPointMake((self.window.frame.size.width) / 2, (self.window.frame.size.height) / 2);
gray.tag = 5;
[self.window addSubview:gray];
[gray release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(change) userInfo:nil repeats:YES];
下面是一个方法:
- (void)change
{
UIView *yellow = [self.window viewWithTag:1];
UIView *red = [self.window viewWithTag:2];
UIView *blue = [self.window viewWithTag:3];
UIView *green = [self.window viewWithTag:4];
UIView *gray = [self.window viewWithTag:5];
UIView *temp = [[UIView alloc] init];
temp.backgroundColor = yellow.backgroundColor;
yellow.backgroundColor = red.backgroundColor;
red.backgroundColor = blue.backgroundColor;
blue.backgroundColor = green.backgroundColor;
green.backgroundColor = gray.backgroundColor;
gray.backgroundColor = temp.backgroundColor;
}
应该就是这些了,基本的小东西,挺好玩的。