1. 開啓一個專案
2. 加入一個雪花的圖片,需要一個背景為透明的雪花圖
3.在mainViewController.m加上程式碼
#import "mainViewController.h"
#define snowSize 25
@interface mainViewController ()
{
UIImage* flakeImage;
}
@end
@implementation mainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1.0];
// load our flake image we will use the same image over and over
flakeImage = [UIImage imageNamed:@"flake.png"];
// start a timet that will fire 20 times per second
[NSTimer scheduledTimerWithTimeInterval:(0.05) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}
// Timer event is called whenever the timer fires
- (void)onTimer
{
// build a view from our flake image
UIImageView* flakeImageView = [[UIImageView alloc] initWithImage:flakeImage];
// use the random() function to randomize up our flake attributes
int startX = round(random() % 734);
int endX = round(random() % 734);
double scale = 1 / round(random() % 100) + 1.0;
double speed = 1 / round(random() % 100) + 1.0;
// set the flake start position
flakeImageView.frame = CGRectMake(startX, -100.0, snowSize * scale, snowSize * scale);
flakeImageView.alpha = 0.25;
// put the flake in our main view
[self.view addSubview:flakeImageView];
[UIView beginAnimations:nil context:(void *)flakeImageView]; //針對雪花的動畫
#define snowSize 25
@interface mainViewController ()
{
UIImage* flakeImage;
}
@end
@implementation mainViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1.0];
// load our flake image we will use the same image over and over
flakeImage = [UIImage imageNamed:@"flake.png"];
// start a timet that will fire 20 times per second
[NSTimer scheduledTimerWithTimeInterval:(0.05) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}
// Timer event is called whenever the timer fires
- (void)onTimer
{
// build a view from our flake image
UIImageView* flakeImageView = [[UIImageView alloc] initWithImage:flakeImage];
// use the random() function to randomize up our flake attributes
int startX = round(random() % 734);
int endX = round(random() % 734);
double scale = 1 / round(random() % 100) + 1.0;
double speed = 1 / round(random() % 100) + 1.0;
// set the flake start position
flakeImageView.frame = CGRectMake(startX, -100.0, snowSize * scale, snowSize * scale);
flakeImageView.alpha = 0.25;
// put the flake in our main view
[self.view addSubview:flakeImageView];
[UIView beginAnimations:nil context:(void *)flakeImageView]; //針對雪花的動畫
// set up how fast the flake will fall
[UIView setAnimationDuration:10 * speed];
// set the postion where flake will move to
flakeImageView.frame = CGRectMake(endX, 880, snowSize * scale, snowSize * scale);
// set a stop callback so we can cleanup the flake when it reaches the
// end of its animation
[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
UIImageView *flakeImageView = (__bridge UIImageView *)(context);
[flakeImageView removeFromSuperview];
// open the debug log and you will see that all flakes have a retain count
// of 1 at this point so we know the release below will keep our memory
// usage in check
//NSLog([NSString stringWithFormat:@"[flakeView retainCount] = %d", [flakeImageView i ]]);
//NSLog([NSString stringWithFormat:@"[flakeView retainCount] = %d", [flakeImageView re]]);
//[flakeImageView release];
flakeImageView = nil;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
4.顯示結果



沒有留言:
張貼留言