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



// 附圖




發佈了26 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章