用UIView写霓虹灯效果。。。NSTimer的使用

首先创建一个视图NioLampViewController类

AppDelegate.h

#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end

AppDelegate.m

#import "AppDelegate.h"
#import "NioLampViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    //创建并且指定一个控制器
    NioLampViewController *mainVC = [[NioLampViewController alloc] init];
    self.window.rootViewController = mainVC;
    
    
    [_window release];
    return YES;
}
- (void)dealloc
{
    [_window release];
    [super dealloc];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end

NioLampViewController.h

#import <UIKit/UIKit.h>
@interface NioLampViewController : UIViewController
@end

NioLampViewController.m

#import "NioLampViewController.h"
@interface NioLampViewController ()
@end
@implementation NioLampViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
   
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 50, 280, 280)];
    view1.backgroundColor = [UIColor yellowColor];
    view1.layer.cornerRadius = 140;
    view1.tag = 100;
    [self.view addSubview:view1];
    [view1 release];
    
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(40, 70, 240, 240)];
    view2.backgroundColor = [UIColor magentaColor];
    view2.layer.cornerRadius = 120;
    view2.tag = 250;
    [self.view addSubview:view2];
    [view2 release];
    
    UIView *view3 = [[UIView alloc] initWithFrame: CGRectMake(60, 90, 200, 200)];
    view3.backgroundColor = [UIColor blueColor];
    view3.layer.cornerRadius = 100;
    view3.tag = 300;
    [self.view addSubview:view3];
    [view3 release];
    
    UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(80, 110, 160, 160)];
    view4.backgroundColor = [UIColor redColor];
    view4.layer.cornerRadius = 80;
    view4.tag = 400;
    [self.view addSubview:view4];
    [view4 release];
    
    UIView *view5 = [[UIView alloc] initWithFrame:CGRectMake(100, 130, 120 , 120)];
    view5.backgroundColor = [UIColor darkGrayColor];
    view5.layer.cornerRadius = 60;
    view5.tag = 500;
    [self.view addSubview:view5];
    [view5 release];
    
    UIView *view6 = [[UIView alloc] initWithFrame:CGRectMake(120, 150, 80, 80)];
    view6.backgroundColor = [UIColor orangeColor];
    view6.layer.cornerRadius = 40;
    view6.tag = 600;
    [self.view addSubview:view6];
    [view6 release];
    
    
    UIView *view7 = [[UIView alloc] initWithFrame:CGRectMake(140, 170, 40, 40)];
    view7.backgroundColor = [UIColor blackColor];
    view7.layer.cornerRadius = 20;
    view7.tag = 700;
    [self.view addSubview:view7];
    [view7 release];
    
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
    
    //    NSTimer *myTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
    //    [myTimer fire];
    //
//        NSTimer *timer = [NSTimer timerWithTimeInterval:0.3 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
//        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    
    
    
    
//    for (int i =0 ; i < 7; i++) {
//        
//        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0 + i *20, 0 + i *20,320 - 40 * i ,568 - i * 40)];
//        
//        CGFloat color = (arc4random() %256 /255.0 );
//        
//        CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;
//        
//        CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;
//        CGFloat brightness1 = ( arc4random() % 56 / 256.0 ) + 0.5;
//
//        view.backgroundColor = [UIColor colorWithHue:color saturation:saturation brightness:brightness alpha:brightness1];
//        
//        [self.view addSubview:view];
//        
//        [view release];
//        
//    }
    
    
    
//    CGFloat color = (arc4random() %256 /255.0 );
//    
//    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;
//    
//    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;
//    CGFloat brightness1 = ( arc4random() % 56 / 256.0 ) + 0.5;
//    view.backgroundColor = [UIColor colorWithHue:color saturation:saturation brightness:brightness alpha:brightness1]
//    
    
    
//    NSArray *array = [NSArray arrayWithObjects:[UIColor redColor], [UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor blueColor], [UIColor cyanColor], [UIColor purpleColor], nil];
    
    
    //方块
    for (int i = 0; i < 11; i++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100 + i * 5, 340 + i * 5, 100 - i * 10, 100 - i * 10)];
       
        CGFloat color = (arc4random() %256 /255.0 );
        CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;
        CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;
        CGFloat brightness1 = ( arc4random() % 56 / 256.0 ) + 0.5;
        view.backgroundColor = [UIColor colorWithHue:color saturation:saturation brightness:brightness alpha:brightness1];
        view.tag = i + 200;
        NSLog(@"%ld", (long)view.tag);
        [self.view addSubview:view];
        [view release];
    }
    
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(roll) userInfo:nil repeats:YES];
    
    
    
    //竖条
    for (int i = 0; i < 10; i++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(300, 40 + i * 40, 20, 40)];
        
        CGFloat color = (arc4random() %256 /255.0 );
        CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;
        CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;
        CGFloat brightness1 = ( arc4random() % 56 / 256.0 ) + 0.5;
        view.backgroundColor = [UIColor colorWithHue:color saturation:saturation brightness:brightness alpha:brightness1];
        view.tag = i + 2000;
        NSLog(@"%ld", (long)view.tag);
        [self.view addSubview:view];
        [view release];
    }
    
    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(shutiao) userInfo:nil repeats:YES];
  
 
}
- (void)shutiao
{
    UIColor *temp = [self.view viewWithTag:200].backgroundColor;
    for (int i = 2000; i < 2010; i++) {
        [self.view viewWithTag:i].backgroundColor = [self.view viewWithTag:i + 1].backgroundColor ;
        
    }
    [self.view viewWithTag:2009].backgroundColor = temp;
}
- (void)roll
{
    UIColor *temp = [self.view viewWithTag:200].backgroundColor;
    for (int i = 200; i < 211; i++) {
       [self.view viewWithTag:i].backgroundColor = [self.view viewWithTag:i + 1].backgroundColor ;
    }
    [self.view viewWithTag:210].backgroundColor = temp;
}
-(void)timerFired
{
    
    
    
    UIView *p1 = [self.view viewWithTag:100];
    UIView *p2 = [self.view viewWithTag:250];
    UIView *p3 = [self.view viewWithTag:300];
    UIView *p4 = [self.view viewWithTag:400];
    UIView *p5 = [self.view viewWithTag:500];
    UIView *p6 = [self.view viewWithTag:600];
    UIView *p7 = [self.view viewWithTag:700];
//    [self.view viewWithTag:100].backgroundColor = [self.view viewWithTag:200].backgroundColor;
    UIColor *temp = p1.backgroundColor;
    p1.backgroundColor = p2.backgroundColor;
    p2.backgroundColor = p3.backgroundColor;
    p3.backgroundColor = p4.backgroundColor;
    p4.backgroundColor = p5.backgroundColor;
    p5.backgroundColor = p6.backgroundColor;
    p6.backgroundColor = p7.backgroundColor;
    p7.backgroundColor = temp;
   
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end
时间: 2024-08-09 22:55:59

用UIView写霓虹灯效果。。。NSTimer的使用的相关文章

android 霓虹灯效果

package com.example.test; import java.util.Timer; import java.util.TimerTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.Menu; import android.widget.TextView; publi

FrameLayout和handle实现霓虹灯效果

这个程序的主要思想就是在一个FrameLayout中定义多个TextView,分别设置不同的背景色.因为帧布局的特性,所以这些控件都是叠加起来的.然后,通过定时器循环给handler发送消息,改变控件的背景色.最后就能实现霓虹灯的效果了,本实例不怎么实用,仅仅能做一般练习而已. 布局文件 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://

帧布局实现霓虹灯效果

Framelayout即帧布局,使用这种布局可以把几个控件叠加在一起.使用Framelayout结合textview就可以实现一个简单的霓虹灯效果. 一.首先在XML中使用了FrameLayout布局并添加六个TestView文件,并设定了颜色和位置. XML布局如下: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.and

利用UIView做出霓虹灯的效果

效果如图 代码如下(只有实现部分) @interface AppDelegate () @end @implementation AppDelegate - (void)dealloc { [_window release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.w

iOS 霓虹灯效果

总体思路如下: 利用for 循环创建N个view,并将他们添加到可变数组中,利用NSTimer,实现循环变色的效果(changeColor:),变色的思路:将第i个view 的背景色 和第i + 1 个view 的背景色交换,换完一轮即可,(NSTimer 会自动实现循环) 这里是在试图控制器中写的,所以 把view 添加到self.view里, self.view.subviews(是一个数组(里边是各个VIEW)) - (void)viewDidLoad {    [super viewDi

IOS开发-UIView之动画效果的实现方法(合集)

http://www.cnblogs.com/GarveyCalvin/p/4193963.html 前言:在开发APP中,我们会经常使用到动画效果.使用动画可以让我们的APP更酷更炫,最重要的是优化用户体验,但取决于动画的质量.像QQ.微信.新浪微博等APP,动画效果就很好了,至少我很喜欢它们的动画,让我使用起来感觉很顺畅,心情很开朗.本文会介绍UIView效果的实现方法,非核心动画. 一.使用UIView类实现动画 基本写法,代码必须放在Begin和Commit之间: [UIView beg

UIVIew之霓虹灯实现

// // AppDelegate.m // NiHongPractice // #import "AppDelegate.h" #define kColorValue arc4random_uniform(256) / 255.0 #define kScreenWidth [UIScreen mainScreen].bounds.size.width #define kScreenHeight [UIScreen mainScreen].bounds.size.height @int

Android突击:FrameLayout制作霓虹灯效果

1,FrameLayout的层叠嵌套,类似于Photoshop的图层.可以实现类似霓虹灯的效果. 代码大致如下: 首先是main.xml: 主要是几个TextView用来填充,使用的是FrameLayout的布局. <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android&quo

iOS--实现UIView的抖动效果-类似Mac上密码输入错误效果

觉得是很不错的一个效果,转来收藏. // 直接传值调用下面的方法就OK,需要的拿走吧 #pragma mark 抖动动画 - (void)shakeAnimationForView:(UIView *) view {        // 获取到当前的View        CALayer *viewLayer = view.layer;        // 获取当前View的位置        CGPoint position = viewLayer.position;        // 移动