(素材源码)swanzhu学IOS(一)UI之_雪花下落


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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值