(素材源碼)swanzhu學IOS(一)UI之_雪花下落

來源:互聯網
上載者:User

標籤:


源碼地址:http://download.csdn.net/detail/swanzhu/8970029

1  定時器加動畫版

////  ZYViewController.m//  ChongYongXuehua1////  Created by mac on 15-8-6.//  Copyright (c) 2015年 zhiyou. All rights reserved.//#import "ZYViewController.h"@interface ZYViewController ()@end@implementation ZYViewController- (void)viewDidLoad{    [super viewDidLoad];    UIImageView  *backImageView=[[UIImageView alloc] initWithFrame:self.view.frame];        backImageView.image=[UIImage imageNamed:@"bg.png"];    [self.view addSubview:backImageView];//    全域變數    array=[[NSMutableArray alloc] initWithCapacity:0];        for (int i=0; i<10; i++) {        UIImageView *snowImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flake.png"]];        snowImageView.frame=CGRectMake(30, -30, 30, 30);        snowImageView.tag=1;        [array addObject:snowImageView];        [self.view addSubview:snowImageView];    }            [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];        }-(void)onTimer{      for (int i=0; i<array.count; i++) {        UIImageView *snow=[array objectAtIndex:i];                if (snow.tag==1) {                        snow.tag=2;            snow.frame=CGRectMake(arc4random()%290, -30, 30, 30);                        [UIView beginAnimations:nil context:(__bridge void *)(snow)];            [UIView setAnimationDuration:3];                        [UIView setAnimationDelegate:self];            [UIView setAnimationDidStopSelector:@selector(a:b:c:)];                        snow.frame=CGRectMake(arc4random()%290, 450, 30, 30);                        [UIView commitAnimations];                        break;        }    }    }-(void)a:(NSString *)name  b:(NSNumber *)num c:(void *)context{    UIImageView *snow=(__bridge UIImageView *)(context);    snow.tag=1;    }- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

2  一個定時器


#import "ZYViewController.h"@interface ZYViewController ()@end@implementation ZYViewController- (void)viewDidLoad{    [super viewDidLoad];UIImageView  *backImageView=[[UIImageView alloc] initWithFrame:self.view.frame];        backImageView.image=[UIImage imageNamed:@"bg.png"];    [self.view addSubview:backImageView];        array=[[NSMutableArray alloc] initWithCapacity:0];        for (int i=0; i<10; i++) {        UIImageView *snowImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flake.png"]];        snowImageView.frame=CGRectMake(30, -30, 30, 30);        snowImageView.tag=1;        [array addObject:snowImageView];        [self.view addSubview:snowImageView];    }            [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];        }-(void)onTimer{    static int count=0;        if (count%20==0) {        for (int i=0; i<array.count; i++) {            UIImageView *snow=[array objectAtIndex:i];            if (snow.tag==1) {                snow.tag=2;                snow.frame=CGRectMake(arc4random()%290, -30, 30, 30);                break;            }        }       }         count++;           for (UIImageView *snow2 in array) {        if (snow2.tag==2) {            snow2.frame=CGRectMake(arc4random()%290, snow2.frame.origin.y+5, 30, 30);                                    if (snow2.frame.origin.y>510) {                snow2.tag=1;            }        }    }}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}

3 用兩個定時器


#import "ZYViewController.h"@interface ZYViewController ()@end@implementation ZYViewController- (void)viewDidLoad{    [super viewDidLoad];UIImageView  *backImageView=[[UIImageView alloc] initWithFrame:self.view.frame];        backImageView.image=[UIImage imageNamed:@"bg.png"];    [self.view addSubview:backImageView];        array=[[NSMutableArray alloc] initWithCapacity:0];        for (int i=0; i<10; i++) {        UIImageView *snowImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flake.png"]];        snowImageView.frame=CGRectMake(30, -30, 30, 30);        snowImageView.tag=1;        [array addObject:snowImageView];        [self.view addSubview:snowImageView];    }            [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(Timer) userInfo:nil repeats:YES];}-(void)onTimer{        static int count=0;        if (count%30==0) {        for (int i=0; i<array.count; i++) {            UIImageView *snow=[array objectAtIndex:i];                                    if (snow.tag==1) {                snow.tag=2;                                snow.frame=CGRectMake(arc4random()%290, -30, 30, 30);                                break;            }        }            }    count++;          }-(void)Timer{    for (int i=0; i<array.count; i++) {             UIImageView *snow=[array objectAtIndex:i];                if (snow.tag==2) {            snow.frame=CGRectMake(arc4random()%290, snow.frame.origin.y+5, 30, 30);        }                if (snow.frame.origin.y>480) {            snow.tag=1;        }    }}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

(素材源碼)swanzhu學IOS(一)UI之_雪花下落

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.