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
// 附图
- IOS 播放雪花的两种方式(代码方式)
- iOS手势 两种播放动画的方式
- Android播放音频的两种方式
- Android播放音频的两种方式
- unity3d两种播放视频的方式
- Android两种播放声音的方式
- 两种方式 创建富文本的代码 ios +
- IOS中打电话的两种方式代码实现
- 【代码笔记】iOS-json文件的两种解析方式
- iOS 播放音频的几种方式
- git提交代码的两种方式
- 代码合并的两种方式详解
- Python 执行代码的两种方式
- 运行js代码的两种方式
- ios应用程序的两种启动方式
- ios打电话的两种方式
- IOS加载视图的两种方式
- iOS 拨打电话的两种方式
- iOS 应用名称的国际化图解
- SAP产品价格
- NoSuchMethodException问题总结
- eclipse打开文件位置插件
- Js setInterval与setTimeout(定时执行与循环执行)的代码(可以传入参数)
- IOS 播放雪花的两种方式(代码方式)
- spring中IOC是什么意思 个人觉得解释的非常有意思
- Java面向对象基础知识
- 设计模式C++实现(2)——策略模式
- 那些年我们一起清除过的浮动
- hdu4597 (博弈dp)play game
- 不能将参数 2 从“TCHAR *”转换为“const char *”
- 毛玻璃效果shader
- 学习笔记(八)对象的集合(三)