IOS-UI学习一,霓虹灯

本博客展示了如何在iOS应用中实现霓虹灯效果,并通过按钮触发从左到右和从右到左的色彩变化,提供了完整的代码实现,包括屏幕布局、背景设置、霓虹灯创建与定时器使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值