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

来源:互联网 发布:d3.js 中文官网 编辑:程序博客网 时间:2024/06/05 03:06

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 {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColorblackColor];

    // 思路

    // 1.准备雪花图片

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

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

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

    

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

    

    [selfloadSelfLayoutSubViews];

    

    

    

}


-(void)loadSelfLayoutSubViews

{

    

    

    imageSnow = [UIImageimageNamed:@"snow"];

    

    arrayImages = [[NSMutableArrayalloc] initWithCapacity:10];

    

    float screenWidth = [[UIScreenmainScreen] 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 = [[UIImageViewalloc] initWithFrame:CGRectMake(x, y, w, w)];

        iv.image =imageSnow;

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

        iv.tag = i ;

        iv.backgroundColor = [UIColorclearColor];

        [self.viewaddSubview:iv];

        

        [arrayImagesaddObject:iv];

        

    }

    

    // 设定timer轮播动画

    

    timer = [NSTimerscheduledTimerWithTimeInterval:0.3target:selfselector:@selector(startBeginAnimation:)userInfo:nilrepeats:YES];

}



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

-(void)startBeginAnimation:(id)sender

{

    

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

    

    index_tag =index_tag + 1;

    

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

        UIImageView *imgv = [arrayImagesobjectAtIndex:0];

        imgv.tag =index_tag ;

        [arrayImagesremoveObjectAtIndex:0];

        

        // 开始动画

        [selfanimationStart1:imgv];

    }

    

}



#pragma mark - 开始动画1

-(void)animationStart1:(UIImageView*)imgv

{

    

    [UIViewanimateWithDuration:6animations:^{

        

        CGRect frame = imgv.frame;

        frame.origin.y = [[UIScreenmainScreen] bounds].size.height;

        imgv.frame = frame;

        

    }completion:^(BOOL finished){

        

        CGRect frame = imgv.frame;

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

        frame.origin.y = -30;

        imgv.frame = frame;

        

        [arrayImagesaddObject:imgv];

    }];

}



#pragma mark - 开始动画2

-(void)animationStart2:(UIImageView*)imgv

{

    [UIViewbeginAnimations:[NSStringstringWithFormat:@"%d",imgv.tag]context:nil];

    [UIViewsetAnimationDuration:6];// 动画时间

    [UIViewsetAnimationDelegate:self];// 动画代理

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

    CGRect frame  = imgv.frame;

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

    [UIViewcommitAnimations];//提交动画

    

}


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


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

{

    NSLog(@"did stop 2");

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

    

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

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

    float y = -30;

    

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

    [arrayImagesaddObject:imageView];

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // 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



// 附图




0 0