霓虹灯(跑马灯、方框赛跑)

直接上代码:

     /*
        代码一: 霓虹灯(跑马灯)
     */

    NSArray *colorArray = [[[NSArray alloc] initWithObjects:[UIColor redColor], [UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor cyanColor], [UIColor blueColor], [UIColor purpleColor], nil] autorelease];
    CGFloat mulValue = [[UIScreen mainScreen] bounds].size.width / 7 ;
    for (int i = 0, j = 0; i < 7; i++,j++) {
        UIView *i = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width - (j * mulValue), [[UIScreen mainScreen] bounds].size.height - (j * mulValue))] ;
        i.backgroundColor = [colorArray objectAtIndex:j];
        i.tag = j + 1 ;
        i.center = self.window.center ;
        [self.window insertSubview:i atIndex:j] ;
        [i release];
    }
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(run) userInfo:nil repeats:YES] ;
    return YES;
}

- (void)run {
    UIColor *acolor = [self.window viewWithTag:7].backgroundColor;
    for (int i = 7; i >= 1; i--) {
        if (i > 1) {
            [self.window viewWithTag:i].backgroundColor = [self.window viewWithTag:(i - 1)].backgroundColor;
        }
        else{
            [self.window viewWithTag:i].backgroundColor = acolor;
        }
    }
}

NSArray *colors = @[[UIColor redColor], [UIColor orangeColor], [UIColor yellowColor], [UIColor greenColor], [UIColor cyanColor], [UIColor blueColor], [UIColor purpleColor]] ;

    for (int i = 0; i < colors.count; i++) {
        UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width - i * 60, self.window.bounds.size.height - i * 60)] ;
        aView.center = self.window.center ;
        aView.backgroundColor = colors[i] ;
        [self.window addSubview:aView] ;
        [aView release] ;
    }

     /*
         代码二:  霓虹灯(跑马灯)
     */

    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(_changBackgroundColor:) userInfo:[self.window subviews].lastObject repeats:YES] ;

    return YES;
}

- (void)_changBackgroundColor:(NSTimer *)sender {

    UIView *currentView = [sender userInfo] ;
    //暂存当前视图的颜色.
    UIColor *tempColor = currentView.backgroundColor ;
    //获取当前视图在子视图数组中的下标
    NSInteger currentIndex = [self.window.subviews indexOfObject:currentView] ;

    for (NSInteger i = currentIndex - 1; i >= 0; i--) {
        UIView *superView = self.window.subviews[i] ;
        currentView.backgroundColor = superView.backgroundColor ;
        currentView = superView ;
    }
    currentView.backgroundColor = tempColor ;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-28 12:33:16

霓虹灯(跑马灯、方框赛跑)的相关文章

关于跑马灯的体会

1. android:singleLine="true"虽然被不建议使用,但是跑马灯必须是它.如果改为android:maxLines="1",不能实现跑马灯效果. 2. android:marqueeRepeatLimit="marquee_forever" 是否使用,没关系. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

Android仿京东首页轮播文字(又名垂直跑马灯)

Android仿京东首页轮播文字(又名垂直跑马灯) 京东客户端的轮播文字效果: 本次要实现的只是后面滚动的文字(前面的用ImageView或者TextView实现即可),看一下实现的效果 实现思路 上图只是一个大概的思路,要实现还需要完善更多的细节,下面会一步步的来实现这个效果: 1.封装数据源:从图上可以看到,轮播的文字是分为两个部分的,暂且把它们分别叫做前缀和内容,而且实际的使用过程中点击轮播图肯定是需要跳转页面的,而且大部分应该是WebView,不妨我们就设置点击时候需要获取的内容就是一个

js简单跑马灯案例

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>跑马灯</title> <style type="text/css"> *{ margin: 0; padding: 0; } #bian{ width: 300px; height: 300px; margin:0 auto

跑马灯《此方法为优化方法,内容不会有闪动效果》

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>跑马灯</title> //css部分 <style type="text/css"> #box { height: 30px; width: 800px; line-height: 30px; background-color:

android 跑马灯

1.在TextView中实现我们的走马灯效果,需要两个属性android:singleLine="true",以及android:ellipsize="marquee": 2.跑马灯效果需要TextVIew获得当前的焦点(focus).然而对于TextView这个控件来说,他的默认的Clickable,LongClickable,Focusable, FocusableInTouchMode这四个属性的值都是false,所以跑马灯效果也就不会出来了,即使你用手触摸T

原生js实现跑马灯抽奖效果

目前好多的微信活动都有一些抽奖活动,其中就有跑马灯. <!DOCTYPE html> <html> <head> <title>跑马灯效果</title> <style> table .pao{ border:1px solid #e5e5e5; padding:10px 20px; } table .on{ border-color:red; color:red; } </style> <script> wi

Android TextView 横向滚动(跑马灯效果)

Android TextView 中当文字比较多时希望它横向滚动显示,下面是一种亲测可行的方法. 效果图: 1.自定义TextView,重写isFocused()方法返回true,让自定义TextView一直处于获取焦点状态. package com.example.shen.marqueedemo; import android.content.Context; import android.util.AttributeSet; import android.widget.TextView;

Android textview 跑马灯 要加的属性

<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/white"  android:ellipsize="marquee"  android:focusable="true" android:singleLine = &q

javascript 跑马灯

1.看了写跑马灯的教程案例,隔了段时间自己写了一个简单的跑马灯.将过程中遇到的问题特此记录下来 代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html;charset=gb2312"> <title>简 陋 的 管 理 后 台 </title> </head> <