源码地址: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.
}