IOS入门——使用UIView简单实现霓虹灯(两种方法)

来源:互联网 发布:仿今日头条网站源码 编辑:程序博客网 时间:2024/06/16 14:30
方法一:简单实现四边框跑马灯
////  AppDelegate.m//  Copyright © 2015年 Treney.com . All rights reserved.//#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    [self.window makeKeyAndVisible];    colors = @[[UIColor redColor],[UIColor blueColor],[UIColor yellowColor],[UIColor orangeColor],[UIColor greenColor],[UIColor grayColor],[UIColor blackColor]];    for (int i=0; i<7; i++) {                UIView *view = [[UIView alloc]initWithFrame:CGRectMake(i*50, 50, 50, 50)];        UIView *view_1 = [[UIView alloc]initWithFrame:CGRectMake(0, i*50+50, 50, 50)];        UIView *view_2 = [[UIView alloc]initWithFrame:CGRectMake(i*50, 400, 50, 50)];        UIView *view_3 = [[UIView alloc]initWithFrame:CGRectMake(270, i*50+50, 50, 50)];        view.tag = 100+i;        view_1.tag=200+i;        view_2.tag=300+i;        view_3.tag=400+i;        view.backgroundColor =colors[arc4random()%colors.count];        view_1.backgroundColor =colors[arc4random()%colors.count];        view_2.backgroundColor =colors[arc4random()%colors.count];        view_3.backgroundColor =colors[arc4random()%colors.count];        [self.window addSubview:view];        [self.window addSubview:view_1];        [self.window addSubview:view_2];        [self.window addSubview:view_3];    }        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(start) userInfo:nil repeats:YES];    return YES;}-(void)start{    UIColor *te = [self.window viewWithTag:100 ].backgroundColor;//查找tag为100的视图,操作te就是操作tag为100的视图,这里设置te是为了实现递进,先保存tag=100的视图的颜色,左后重新赋值给最后一个视图    UIColor *te_1 = [self.window viewWithTag:200 ].backgroundColor;    UIColor *te_2 = [self.window viewWithTag:300 ].backgroundColor;    UIColor *te_3 = [self.window viewWithTag:400 ].backgroundColor;    for (int i = 0; i <6; i++) {        [self.window viewWithTag:100 +i ].backgroundColor =  [self.window viewWithTag:101 +i ].backgroundColor;        [self.window viewWithTag:200 +i ].backgroundColor =  [self.window viewWithTag:201 +i ].backgroundColor;        [self.window viewWithTag:300 +i ].backgroundColor =  [self.window viewWithTag:301 +i ].backgroundColor;        [self.window viewWithTag:400 +i ].backgroundColor =  [self.window viewWithTag:401 +i ].backgroundColor;    }        [self.window viewWithTag:106 ].backgroundColor = te;    [self.window viewWithTag:206 ].backgroundColor = te_1;    [self.window viewWithTag:306 ].backgroundColor = te_2;    [self.window viewWithTag:406 ].backgroundColor = te_3;}

方法二:实现霓虹灯效果等闪烁


////  AppDelegate.m//  地方//  Copyright © 2015年 Treney.com. All rights reserved.//#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{     [self.window makeKeyAndVisible];    //定义颜色数组    NSMutableArray *arr = [NSMutableArray arrayWithObjects:[UIColor redColor], [UIColor yellowColor],[UIColor blueColor],[UIColor greenColor],[UIColor grayColor],[UIColor purpleColor],[UIColor orangeColor],nil];        //for循环 设定位置   颜色    for (int i = 0 ; i < 7; i++) {        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(i * 20,  i *20, 320 - 40 * i , 568 - i * 40)];//x y轴依次改变,宽高依次减小    xy越大   宽高越小        view.tag = 100 + i;//设置tag        view.backgroundColor = arr[i];//设置视图颜色(从可变数组arr中读取)        [self.window addSubview:view];//添加到主窗口            }        [NSTimer scheduledTimerWithTimeInterval:0.23 target:self selector:@selector(time) userInfo:nil  repeats:YES];    self.window.backgroundColor = [UIColor whiteColor];    return YES;}- (void)time{    UIColor *te = [self.window viewWithTag:100 ].backgroundColor;//查找tag为100的视图,操作te就是操作tag为100的视图,这里设置te是为了实现递进,先保存tag=100的视图的颜色,左后重新赋值给最后一个视图    for (int i = 0; i <6; i++) {        [self.window viewWithTag:100 +i ].backgroundColor =  [self.window viewWithTag:101 +i ].backgroundColor;    }    [self.window viewWithTag:106 ].backgroundColor = te;}



0 0