首页 > 代码库 > 实现霓虹灯闪烁效果(UI)
实现霓虹灯闪烁效果(UI)
//延展添加对象
@interface AppDelegate ()
{
UIView *_containerView;
NSTimer *_timer;
}
@end
//- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中函数体//
_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];
}
实现霓虹灯闪烁效果(UI)