热门标签 | 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

 


推荐阅读
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • Windows服务与数据库交互问题解析
    本文探讨了在Windows 10(64位)环境下开发的Windows服务,旨在定期向本地MS SQL Server (v.11)插入记录。尽管服务已成功安装并运行,但记录并未正确插入。我们将详细分析可能的原因及解决方案。 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • PyCharm下载与安装指南
    本文详细介绍如何从官方渠道下载并安装PyCharm集成开发环境(IDE),涵盖Windows、macOS和Linux系统,同时提供详细的安装步骤及配置建议。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
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社区 版权所有