-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnowFallView.m
executable file
·115 lines (97 loc) · 3.69 KB
/
SnowFallView.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//
// SnowFallView.m
// SnowFall
//
// Created by Felice on 03/11/10.
//
#import "SnowFallView.h"
#import <QuartzCore/QuartzCore.h>
#define kDefaultFlakesCount 200
#define kDefaultFlakeWidth 40.0
#define kDefaultFlakeHeight 46.0
#define kDefaultFlakeFileName @"narwhal.png"
#define kDefaultMinimumSize 0.4
#define kDefaultAnimationDurationMin 6.0
#define kDefaultAnimationDurationMax 12.0
@implementation SnowFallView
@synthesize flakesArray;
@synthesize flakesCount;
@synthesize flakeWidth;
@synthesize flakeHeight;
@synthesize flakeFileName;
@synthesize flakeMinimumSize;
@synthesize animationDurationMin;
@synthesize animationDurationMax;
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// avoid drawing outside the view bounds
self.clipsToBounds = YES;
// set default values
flakesCount = kDefaultFlakesCount;
flakeWidth = kDefaultFlakeWidth;
flakeHeight = kDefaultFlakeHeight;
flakeFileName = kDefaultFlakeFileName;
flakeMinimumSize = kDefaultMinimumSize;
animationDurationMin = kDefaultAnimationDurationMin;
animationDurationMax = kDefaultAnimationDurationMax;
}
return self;
}
-(void)createFlakes {
srandomdev();
flakesCount = 300;
flakesArray = [[NSMutableArray alloc] initWithCapacity:flakesCount];
UIImage *flakeImg = [UIImage imageNamed:flakeFileName];
for (int i=0; i < flakesCount; i++) {
// random flake size, range: from 40% to 100% actual size
float vz = ((Float32)1.0*(Float32)random()/(Float32)RAND_MAX);
// check size low boundary limit
vz = (vz < flakeMinimumSize ? flakeMinimumSize : vz);
float vw = flakeWidth*vz;
float vh = flakeHeight*vz;
// ensure flakes span beyond the view right bound, by adding flake width
// creates the "window" effect with half flakes on the border
float vx = ((Float32)self.frame.size.width*(Float32)random()/(Float32)RAND_MAX);
// enlarge content height by 1/2 view height, screen is always well populated
float vy = ((Float32)self.frame.size.height*1.5*(Float32)random()/(Float32)RAND_MAX);
// flakes start y position is above upper view bound, add view height
vy += self.frame.size.height;
// ensure flakes span beyond the view left bound, by subtracting flake width
vx -= vw;
CGRect frame = CGRectMake(vx, vy, vw, vh);
UIImageView *imageView = [[UIImageView alloc] initWithImage:flakeImg];
imageView.frame = frame;
imageView.userInteractionEnabled = NO;
[flakesArray addObject:imageView];
[self addSubview:imageView];
}
}
-(void) letItSnow {
if (!flakesArray)
[self createFlakes];
self.backgroundColor = [UIColor clearColor];
// prepare rotation animation
CABasicAnimation *rotAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
rotAnimation.repeatCount = 1e100;
rotAnimation.autoreverses = NO;
// 360 degrees in radians
rotAnimation.toValue = [NSNumber numberWithFloat:6.28318531];
// prepare vertical fall animation
CABasicAnimation *theAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
theAnimation.repeatCount=1e100;
theAnimation.autoreverses=NO;
for (UIImageView *v in flakesArray) {
CGPoint p = v.center;
float startypos = p.y;
float endypos = self.frame.size.height;
p.y = endypos;
v.center = p;
float timeInterval = ((Float32)(animationDurationMax-animationDurationMin)*(Float32)random()/(Float32)RAND_MAX);
theAnimation.duration=timeInterval+animationDurationMin;
theAnimation.fromValue=[NSNumber numberWithFloat:-startypos];
[v.layer addAnimation:theAnimation forKey:@"transform.translation.y"];
rotAnimation.duration=timeInterval;
[v.layer addAnimation:rotAnimation forKey:@"transform.rotation.y"];
}
}
@end