IOS-UI学习一,霓虹灯

来源:互联网 发布:js实现购物车 编辑:程序博客网 时间:2024/04/29 06:41

1.新建一个类HomeViewController.m继承于viewController,并且在AppDelegate.m文件中导入

#import "AppDelegate.h"
#import "HomeViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    //声明
    HomeViewController *homeVC = [HomeViewController new];
    self.window.rootViewController = homeVC;

    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}


2.在HomeViewController.m文件写代码

//霓虹灯

#import "HomeViewController.h"

@interface HomeViewController ()

//三个全局变量,可以在HomeViewController.m文件里面使用
{
    UIView *view;
    NSArray *neonLightColorArray;
    NSTimer *timer;
}
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //屏幕大小
    CGFloat screen_width = [[UIScreen mainScreen] bounds].size.width;
    CGFloat screen_height = [[UIScreen mainScreen] bounds].size.height;
    
    //设置背景颜色
    UIView *backGroundView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    backGroundView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:backGroundView];
    
    //霓虹灯大小
    CGFloat x = (screen_width - 40)/7;
    
    //创建霓虹灯
    neonLightColorArray = [[NSArray alloc] initWithObjects:[UIColor redColor],[UIColor orangeColor],[UIColor yellowColor],[UIColor greenColor],[UIColor cyanColor],[UIColor blueColor],[UIColor purpleColor], nil];
    for (int i = 0 ; i < 7 ; i++) {
        view = [[UIView alloc] initWithFrame:CGRectMake(x/2+x*i, screen_height/8, x, x)];
        view.backgroundColor = neonLightColorArray[i];
        view.tag = 100+i;
        [self.view addSubview:view];
    }
    
    
    //创建按钮(颜色从右到左改变颜色)
    UIButton *leftToRightStratButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftToRightStratButton.frame = CGRectMake(x/2, screen_height/4, 2*x, x);
    leftToRightStratButton.backgroundColor = [UIColor whiteColor];
    [leftToRightStratButton setTitle:@"从左到右" forState:0];
    [leftToRightStratButton setTitleColor:[UIColor blackColor] forState:0];
    [leftToRightStratButton addTarget:self action:@selector(leftToRightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    leftToRightStratButton.layer.cornerRadius = 15;
    [self.view addSubview:leftToRightStratButton];
    
    //创建按钮(颜色从左到右改变)
    UIButton *rightToLeftStartButton = [UIButton buttonWithType:UIButtonTypeSystem];
    rightToLeftStartButton.frame = CGRectMake(5*x, screen_height/4, 2*x, x);
    rightToLeftStartButton.backgroundColor = [UIColor whiteColor];
    [rightToLeftStartButton setTitle:@"从右到左" forState:0];
    [rightToLeftStartButton setTitleColor:[UIColor blackColor] forState:0];
    [rightToLeftStartButton addTarget:self action:@selector(rightToLeftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    rightToLeftStartButton.layer.cornerRadius = 15;
    [self.view addSubview:rightToLeftStartButton];
 
}

//从右到左按钮触发事件
-(void)rightToLeftButtonAction:(UIButton *)sender {
    [timer invalidate];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(rightToLeft) userInfo:nil repeats:YES];
}

//颜色从右到左改变
-(void)rightToLeft {
    UIView *temp = [[UIView alloc] init];
    int i = 100;
    temp.backgroundColor = [self.view viewWithTag:100].backgroundColor;
    for ( ; i < 106 ; i++) {
        [self.view viewWithTag:i].backgroundColor = [self.view viewWithTag:1+i].backgroundColor;
    }
    [self.view viewWithTag:106].backgroundColor = temp.backgroundColor;
}

//从左到右按钮触发事件
-(void)leftToRightButtonAction:(UIButton *)sender {
    [timer invalidate];
    timer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(leftToRight) userInfo:nil repeats:YES];
}

//颜色从左到右改变
-(void)leftToRight {
    UIView *temp = [[UIView alloc] init];
    int i = 106;
    temp.backgroundColor = [self.view viewWithTag:106].backgroundColor;
    for ( ; i >99 ; i--) {
        [self.view viewWithTag:i].backgroundColor = [self.view viewWithTag:i-1].backgroundColor;
    }
    [self.view viewWithTag:100].backgroundColor = temp.backgroundColor;
}





0 0
原创粉丝点击