IOS 霓虹燈簡單小程式

來源:互聯網
上載者:User

標籤:style   blog   class   code   java   tar   

在RootViewController.m檔案中

- (void)viewDidLoad//視圖載入方法

- (void)viewDidLoad{        //設定紅色    UIView *viewRed = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 120, 30)];    viewRed.backgroundColor = [UIColor redColor];    [self.view addSubview:viewRed];        //設定橙色    UIView *viewOrange = [[UIView alloc]initWithFrame:CGRectMake(100, 130, 120, 30)];    viewOrange.backgroundColor = [UIColor orangeColor];    [self.view addSubview:viewOrange];        //設定黃色       UIView *viewYellow = [[UIView alloc]initWithFrame:CGRectMake(100, 160, 120, 30)];    viewYellow.backgroundColor = [UIColor yellowColor];    [self.view addSubview:viewYellow];        //設定綠色    UIView *viewGreen = [[UIView alloc]initWithFrame:CGRectMake(100, 190, 120, 30)];    viewGreen.backgroundColor = [UIColor greenColor];    [self.view addSubview:viewGreen];        //設定青色    UIView *viewGray = [[UIView alloc]initWithFrame:CGRectMake(100, 220, 120, 30)];    viewGray.backgroundColor = [UIColor grayColor];    [self.view addSubview:viewGray];        //設定藍色    UIView *viewBlue = [[UIView alloc]initWithFrame:CGRectMake(100, 250, 120, 30)];    viewBlue.backgroundColor = [UIColor blueColor];    [self.view addSubview:viewBlue];    //設定紫色    UIView *viewPurple = [[UIView alloc]initWithFrame:CGRectMake(100, 280, 120, 30)];    viewPurple.backgroundColor = [UIColor purpleColor];    [self.view addSubview:viewPurple];        //設定按鈕    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];    button.frame = CGRectMake(100, 330, 120, 30);    [button setTitle:@"開始" forState:UIControlStateNormal];    button.backgroundColor = [UIColor blackColor];    button.tag = 108;    [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:button];        [super viewDidLoad];    // Do any additional setup after loading the view.}

在關聯方法btnAction中:

 timer為全域變數

 可以在RootViewController.h檔案中定義;

-(void)btnAction:(id)sender{    UIButton *button = (UIButton*)sender;    if (button.tag ==108) {        timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(doChangeColor:) userInfo:nil repeats:YES];        [timer fire];//計時器開始    }    }

在關聯方法doChangeColor中:

-(void)doChangeColor:(id)sender{    NSArray *arr = self.view.subviews;//得到所有的視圖    int count = [arr count]-1;//得到計數    UIView *firstView = [arr objectAtIndex:0];    UIColor *firstColor = firstView.backgroundColor;        for (int i=0; i<count; i++) {        UIView *view = [arr objectAtIndex:i];        UIView *nextView = [arr objectAtIndex:(i+1)];        view.backgroundColor = nextView.backgroundColor;    }    ((UIView*)[arr objectAtIndex:count]).backgroundColor = firstColor;    self.view.backgroundColor = [UIColor colorWithRed:(arc4random()%255/255.0) green:(arc4random()%255/255.0) blue:(arc4random()%255/255.0) alpha:(arc4random()%255/255.0)];    }

結果顯示:

 

 會不斷的改變背景圖片,和實現了霓虹燈的效果。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.