IOS 播放雪花的两种方式(代码方式)

IOS 播放雪花的两种方式(代码方式)

代码:


====================> .h 文件

//

//  WWCShowSnowViewController.h

//  TestCAOrUIViewAnimationApp7-30

//

//  Created by Whitney.c on 15/7/30.

//  Copyright (c) 2015 ZhongShan Sun union Medical Technology Co. Ltd. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface WWCShowSnowViewController : UIViewController

{

    

    UIImage *imageSnow; // 雪花图片

    

    NSMutableArray *arrayImages; // 装载雪花的集合

    

    NSTimer *timer; // timer 控制反复播放及播放速度

    

}

@end



====================> .m 文件


//

//  WWCShowSnowViewController.m

//  TestCAOrUIViewAnimationApp7-30

//

//  Created by Whitney.c on 15/7/30.

//  Copyright (c) 2015 ZhongShan Sun union Medical Technology Co. Ltd. All rights reserved.

//


#import "WWCShowSnowViewController.h"


@interface WWCShowSnowViewController ()


@end


@implementation WWCShowSnowViewController



static int index_tag = 0;


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor blackColor];

    // 思路

    // 1.准备雪花图片

    // 2.准备装载雪花的集合

    // 3.集合中装载UIImageView(雪花),设置好透明度,X(随机)Y固定在屏幕上放即可,W/H 随机生成

    // 4.结束动画,加入一个新的雪花(只改变X-》防止动画看起来重复

    

    // 分别加入了2种方式, 代码粗糙,希望能有帮助

    

    [self loadSelfLayoutSubViews];

    

    

    

}


-(void)loadSelfLayoutSubViews

{

    

    

    imageSnow = [UIImage imageNamed:@"snow"];

    

    arrayImages = [[NSMutableArray alloc] initWithCapacity:10];

    

    float screenWidth = [[UIScreen mainScreen] bounds].size.width;

    

    for (int i = 0; i < 20; i++) {

        // 随机生成的x

        

        float x = arc4random()%(int)screenWidth;

        float w = (arc4random()%20)+ 10 ;

        float y = -30;

        NSLog(@"x:%f,y:%f,w:%f",x,y,w);

        UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, w)];

        iv.image = imageSnow;

        iv.alpha = ((float)(arc4random()%10))/10;

        iv.tag = i ;

        iv.backgroundColor = [UIColor clearColor];

        [self.view addSubview:iv];

        

        [arrayImages addObject:iv];

        

    }

    

    // 设定timer 轮播动画

    

    timer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(startBeginAnimation:) userInfo:nil repeats:YES];

}



#pragma mark - 动画开始前的准备

-(void)startBeginAnimation:(id)sender

{

    

    NSLog(@" start begin Animation .....");

    

    index_tag = index_tag + 1;

    

    if ( arrayImages && arrayImages.count >0) {

        UIImageView *imgv = [arrayImages objectAtIndex:0];

        imgv.tag = index_tag ;

        [arrayImages removeObjectAtIndex:0];

        

        // 开始动画

        [self animationStart1:imgv];

    }

    

}



#pragma mark - 开始动画1

-(void)animationStart1:(UIImageView*)imgv

{

    

    [UIView animateWithDuration:6 animations:^{

        

        CGRect frame = imgv.frame;

        frame.origin.y = [[UIScreen mainScreen] bounds].size.height;

        imgv.frame = frame;

        

    }completion:^(BOOL finished){

        

        CGRect frame = imgv.frame;

        frame.origin.x = arc4random()%(int)([[UIScreen mainScreen] bounds].size.width);

        frame.origin.y = -30;

        imgv.frame = frame;

        

        [arrayImages addObject:imgv];

    }];

}



#pragma mark - 开始动画2

-(void)animationStart2:(UIImageView*)imgv

{

    [UIView beginAnimations:[NSString stringWithFormat:@"%d",imgv.tag] context:nil];

    [UIView setAnimationDuration:6]; // 动画时间

    [UIView setAnimationDelegate:self]; // 动画代理

    // 要调整的Frame(原始的Frame到次Frame的过度动画)

    CGRect frame  = imgv.frame;

    imgv.frame = CGRectMake(frame.origin.x, [[UIScreen mainScreen] bounds].size.height, frame.size.width, frame.size.height);

    [UIView commitAnimations];// 提交动画

    

}


#pragma mark - 动画2代理实现-> 当动画停止时


- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)contex

{

    NSLog(@"did stop 2");

    UIImageView *imageView = (UIImageView *)[self.view viewWithTag:[animationID integerValue]];

    

    float x = arc4random()%(int)([[UIScreen mainScreen] bounds].size.width);

    float w = (arc4random()%20)+ 10 ;

    float y = -30;

    

    imageView.frame = CGRectMake(x, y, w, w);

    [arrayImages addObject:imageView];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end



// 附图




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值