热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iOSsnowanimation

CTSnowAnimationView.hCTMyCtripCreatedbyalexon1614.Copyright©2016年ctrip.Allrightsreserved.#

//
// CTSnowAnimationView.h
// CTMyCtrip
//
// Created by alex on 16/1/4.
// Copyright © 2016年 ctrip. All rights reserved.
//

#import
@interface CTSnowAnimationView : UIView
-(id)initWithFrame:(CGRect)frame;
-(void)startSnowWithFlakesNumber:(int)num duration:(float)duration;
-(void)stopSnowAnimation;
-(void)removeSnowAnimation;
@end


//
// CTSnowAnimationView.m
// CTMyCtrip
//
// Created by alex on 16/1/4.
// Copyright © 2016年 ctrip. All rights reserved.
//

#import "CTSnowAnimationView.h"
#define REPEATSNOW @"geneRepeatSnow"
@interface CTSnowAnimationView()
@property (strong, nonatomic) NSMutableArray
*snowFlakes;
@end
@implementation CTSnowAnimationView
-(id)initWithFrame:(CGRect)frame{
self
= [super initWithFrame:frame];
if (self) {
return self;
}
return [[CTSnowAnimationView alloc] init];
}
-(void)removeSnowAnimation{
for (UIView *flake in self.snowFlakes) {
[flake removeFromSuperview];
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:REPEATSNOW
object:nil];
self.snowFlakes
= nil;
}
-(void)startSnowWithFlakesNumber:(int)num duration:(float)duration{
dispatch_async(dispatch_get_main_queue(),
^{
if ([self.snowFlakes count] != num) {
if ([self.snowFlakes count] != 0) {
for (UIView *subview in self.snowFlakes) {
[subview removeFromSuperview];
}
}
self.snowFlakes
= [NSMutableArray array];
for (int i = 0; i ) {
UIImageView *tmpView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myctrip_snowflake.png"]];
tmpView.frame
= CGRectZero;
tmpView.userInteractionEnabled
= FALSE;
[self addSubview:tmpView];
[self.snowFlakes addObject:tmpView];
}
}
[self showFlakes:YES];

[[NSNotificationCenter defaultCenter] removeObserver:self name:REPEATSNOW
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveRepeatNotification:) name:REPEATSNOW
object:nil];
for (UIView *flake in self.snowFlakes) {
[flake.layer removeAllAnimations];
}
for (int i = 0; i <[self.snowFlakes count]; i++) {
[self singleFlakeSnow:i withDuration:duration];
}
});
}
-(void)stopSnowAnimation{
for (UIView *flake in self.snowFlakes) {
[flake.layer removeAllAnimations];
}
[self showFlakes:
false];
}
- (float)randomFloatBetween:(float)min and:(float)max {
float diff = max - min;
return (((float) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * diff) + min;
}
-(void)showFlakes:(BOOL)need{
for (UIImageView *flake in self.snowFlakes) {
flake.hidden
= !need;
}
}
-(void)singleFlakeSnow:(int)index withDuration:(float)duration{
UIView
*flake = self.snowFlakes[index];
int size = [self randomFloatBetween:8.0 and:14.0];
CGRect flakeFrame
= flake.frame;
[flake.layer removeAllAnimations];
flake.layer.opacity
= 0.1;
flakeFrame.origin.x
= arc4random_uniform(CTScreenWidth);
flakeFrame.origin.y
= -size;
flakeFrame.size.width
= size;
flakeFrame.size.height
= size;
flake.frame
= flakeFrame;
float delay = arc4random_uniform(5.0);
[self layoutIfNeeded];
[UIView animateWithDuration:duration delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:
^{
flake.frame
= CGRectMake(flakeFrame.origin.x + 40 - arc4random_uniform(80), self.bounds.size.height, size, size);
flake.layer.opacity
= 1.0;
[self layoutIfNeeded];
} completion:
^(BOOL finished) {
if (finished) {
[[NSNotificationCenter defaultCenter] postNotificationName:REPEATSNOW
object:nil userInfo:@{@"index":[NSNumber numberWithInt:index],
@"duration":[NSNumber numberWithFloat:duration]
}];
}
}];
}
-(void)receiveRepeatNotification:(NSNotification *)notification{
if ([[notification name] isEqualToString:REPEATSNOW]) {
int index = [((NSNumber*)[notification.userInfo objectForKey:@"index"]) intValue];
float duration = [((NSNumber*)[notification.userInfo objectForKey:@"duration"]) floatValue];
[self singleFlakeSnow:index withDuration:duration];
}
}
@end

Using AutoLayout:

 

技术分享技术分享

1 - (float)randomFloatBetween:(float)min and:(float)max {
2 float diff = max - min;
3 return (((float) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * diff) + min;
4 }
5
6 -(void)showFlakes:(BOOL)need{
7 for (UIImageView *flake in self.snowFlakes) {
8 flake.hidden = !need;
9 }
10 }
11
12 -(void)singleFlakeSnow:(int)index{
13 UIView *flake = self.snowFlakes[index];
14 NSLayoutConstraint *x = self.flakesHorizonalPosition[index];
15 NSLayoutConstraint *y = self.flakesVerticalPosition[index];
16 int size = [self randomFloatBetween:8.0 and:14.0];
17 ((NSLayoutConstraint*)self.flakesWidth[index]).cOnstant= size;
18 ((NSLayoutConstraint*)self.flakesHeight[index]).cOnstant= size;
19 [flake.layer removeAllAnimations];
20 flake.layer.opacity = 0.1;
21 x.cOnstant= arc4random_uniform(CTScreenWidth);
22 y.cOnstant= -size;
23 float delay = arc4random_uniform(5.0);
24 [self layoutIfNeeded];
25 [UIView animateWithDuration:7.0 delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:^{
26 x.cOnstant= x.constant + 40 - arc4random_uniform(80);
27 y.cOnstant= self.bounds.size.height;
28 flake.layer.opacity = 1.0;
29 [self layoutIfNeeded];
30 } completion:^(BOOL finished) {
31 if (finished) {
32 [[NSNotificationCenter defaultCenter] postNotificationName:REPEATSNOW object:nil userInfo:@{@"index":[NSNumber numberWithInt:index]}];
33 }
34 }];
35 }
36
37 -(void)snowAnimation:(BOOL)need{
38 if (need ) {
39 dispatch_async(dispatch_get_main_queue(), ^{
40 [self showFlakes:YES];
41 self.snowGround.hidden = FALSE;
42 self.snowBackground.hidden = FALSE;
43 [[NSNotificationCenter defaultCenter] removeObserver:self name:REPEATSNOW object:nil];
44 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveRepeatNotification:) name:REPEATSNOW object:nil];
45 for (UIView *flake in self.snowFlakes) {
46 [flake.layer removeAllAnimations];
47 }
48 for (int i = 0; i <[self.snowFlakes count]; i++) {
49 [self singleFlakeSnow:i];
50 }
51 });
52 }
53 else{
54 dispatch_async(dispatch_get_main_queue(), ^{
55 [[NSNotificationCenter defaultCenter] removeObserver:self name:REPEATSNOW object:nil];
56 for (UIView *flake in self.snowFlakes) {
57 [flake.layer removeAllAnimations];
58 }
59 [self showFlakes:NO];
60 });
61 }
62 }
63
64 -(void)receiveRepeatNotification:(NSNotification *)notification{
65 if ([[notification name] isEqualToString:REPEATSNOW]) {
66 int index = [((NSNumber*)[notification.userInfo objectForKey:@"index"]) intValue];
67 [self singleFlakeSnow:index];
68 }
69 }


View Code

 


推荐阅读
  • vue引入echarts地图的四种方式
    一、vue中引入echart1、安装echarts:npminstallecharts--save2、在main.js文件中引入echarts实例:  Vue.prototype.$echartsecharts3、在需要用到echart图形的vue文件中引入:   importechartsfrom&amp;quot;echarts&amp;quot;;4、如果用到map(地图),还 ... [详细]
  • 本文介绍了 Go 语言中的高性能、可扩展、轻量级 Web 框架 Echo。Echo 框架简单易用,仅需几行代码即可启动一个高性能 HTTP 服务。 ... [详细]
  • 包含phppdoerrorcode的词条 ... [详细]
  • 如果应用程序经常播放密集、急促而又短暂的音效(如游戏音效)那么使用MediaPlayer显得有些不太适合了。因为MediaPlayer存在如下缺点:1)延时时间较长,且资源占用率高 ... [详细]
  • 面试题总结_2019年全网最热门的123个Java并发面试题总结
    面试题总结_2019年全网最热门的123个Java并发面试题总结 ... [详细]
  • WPF项目学习.一
    WPF项目搭建版权声明:本文为博主初学经验,未经博主允许不得转载。一、前言记录在学习与制作WPF过程中遇到的解决方案。使用MVVM的优点是数据和视图分离,双向绑定,低耦合,可重用行 ... [详细]
  • 本文将深入探讨 iOS 中的 Grand Central Dispatch (GCD),并介绍如何利用 GCD 进行高效多线程编程。如果你对线程的基本概念还不熟悉,建议先阅读相关基础资料。 ... [详细]
  • Python多线程详解与示例
    本文介绍了Python中的多线程编程,包括僵尸进程和孤儿进程的概念,并提供了具体的代码示例。同时,详细解释了0号进程和1号进程在系统中的作用。 ... [详细]
  • 2020年9月15日,Oracle正式发布了最新的JDK 15版本。本次更新带来了许多新特性,包括隐藏类、EdDSA签名算法、模式匹配、记录类、封闭类和文本块等。 ... [详细]
  • 我有一个从C项目编译的.o文件,该文件引用了名为init_static_pool ... [详细]
  • Java 中的等时日期(int,int)方法,示例 ... [详细]
  • 本文介绍了如何使用Python爬取妙笔阁小说网仙侠系列中所有小说的信息,并将其保存为TXT和CSV格式。主要内容包括如何构造请求头以避免被网站封禁,以及如何利用XPath解析HTML并提取所需信息。 ... [详细]
  • 兆芯X86 CPU架构的演进与现状(国产CPU系列)
    本文详细介绍了兆芯X86 CPU架构的发展历程,从公司成立背景到关键技术授权,再到具体芯片架构的演进,全面解析了兆芯在国产CPU领域的贡献与挑战。 ... [详细]
  • 深入解析Django CBV模型的源码运行机制
    本文详细探讨了Django CBV(Class-Based Views)模型的源码运行流程,通过具体的示例代码和详细的解释,帮助读者更好地理解和应用这一强大的功能。 ... [详细]
  • 本文将介绍如何在混合开发(Hybrid)应用中实现Native与HTML5的交互,包括基本概念、学习目标以及具体的实现步骤。 ... [详细]
author-avatar
史三万岁_710
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有