1 ViewController.h 2 3 #import <UIKit/UIKit.h> 4 5 @interface ViewController : UIViewController{ 6 UIImage* flakeImage; 7 } 8 - (void)onTimer; 9 @end
1 ViewController.m 2 3 #import "ViewController.h" 4 5 @interface ViewController () 6 7 @end 8 9 @implementation ViewController 10 11 - (void)viewDidLoad 12 { 13 [super viewDidLoad]; 14 self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1.0]; 15 flakeImage = [UIImage imageNamed:@"2.png"]; 16 [NSTimer scheduledTimerWithTimeInterval:(0.05) target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; 17 } 18 - (void)onTimer 19 { 20 //创建图像视图对象 21 UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage]; 22 23 int startX = round(random() % 320); 24 int endX = round(random() % 320); 25 double scale = 1 / round(random() % 100) + 1.0; 26 double speed = 1 / round(random() % 100) + 1.0; 27 flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale); 28 flakeView.alpha = 0.5; 29 [self.view addSubview:flakeView]; 30 [UIView beginAnimations:nil context:(__bridge void *)(flakeView)]; 31 [UIView setAnimationDuration:5 * speed]; 32 flakeView.frame = CGRectMake(endX, 540.0, 25.0 * scale, 25.0 * scale); 33 [UIView commitAnimations]; 34 35 } 36 37 38 - (void)didReceiveMemoryWarning 39 { 40 [super didReceiveMemoryWarning]; 41 // Dispose of any resources that can be recreated. 42 }
图片素材,通过Main.storyboard设置背景图片