Heavy snow in iOS animation and heavy snow in ios
1. Result Display
The beautiful snowflake reminds you of the wonderful memories of life. Pai_^
2. Production ideas
In fact, it is very simple to create such a university scenario. It is as simple as you can't think of it after reading the tutorial. OK. The following international practices will explain your ideas.
1. Create an array to save a large number of snowflakes
_imagesArray = [[NSMutableArray alloc] init]; for (int i = 0; i < 1000; ++ i) { UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snow"]]; float x = IMAGE_WIDTH; imageView.frame = CGRectMake(IMAGE_X, -30, x, x); imageView.alpha = IMAGE_ALPHA; [self.view addSubview:imageView]; [_imagesArray addObject:imageView]; }
2. Use the clock (CADisplayLink) to control the snow. Why not use NSTimer. Actually, it is okay, but (CADisplayLink) the frame is faster.
// Create a clock and add it to the main loop. CADisplayLink * link = [CADisplayLink displayLinkWithTarget: self selector: @ selector (makeSnow)]; [link addToRunLoop: [nsunloop mainRunLoop] forMode: NSDefaultRunLoopMode];
3. When it snows, the array is used as a queue.
- (void)makeSnow{ if (_imagesArray.count > 0) { UIImageView *imageView = _imagesArray[0]; [_imagesArray removeObjectAtIndex:0]; [self snowFall:imageView]; }}- (void)snowFall:(UIImageView *)imageView{ [UIView animateWithDuration:10 animations:^{ imageView.frame = CGRectMake(imageView.frame.origin.x, Main_Screen_Height, imageView.frame.size.width, imageView.frame.size.height); imageView.transform = CGAffineTransformMakeScale(0.3, 0.3); imageView.transform = CGAffineTransformRotate(imageView.transform, M_PI); } completion:^(BOOL finished) { float x = IMAGE_WIDTH; imageView.frame = CGRectMake(IMAGE_X, -30, x, x); [_imagesArray addObject:imageView]; }];}
3. Code and truth
# Define IMAGE_X arc4random () % (int) Main_Screen_Width # define IMAGE_ALPHA (float) (arc4random () % 10)/10 # define IMAGE_WIDTH arc4random () % 20 + 10 # define PLUS_HEIGHT Main_Screen_Height/25 # define Main_Screen_Height [[UIScreen mainScreen] bounds]. size. height # define Main_Screen_Width [[UIScreen mainScreen] bounds]. size. width # import "ViewController. h "@ interface ViewController () @ property (nonatomic, strong) NSMutableArray * imagesArray; @ property (nonatomic, strong) UIImageView * imageView; @ end @ implementation ViewController-(void) loadView {UIImageView * imageView = [[UIImageView alloc] initWithFrame: [UIScreen mainScreen]. bounds]; imageView. image = [UIImage imageNamed: @ "backgound.jpg"]; imageView. contentMode = UIViewContentModeScaleAspectFill; self. view = imageView;}-(void) viewDidLoad {[super viewDidLoad]; _ imagesArray = [[NSMutableArray alloc] init]; for (int I = 0; I <1000; ++ I) {UIImageView * imageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ "snow"]; float x = IMAGE_WIDTH; imageView. frame = CGRectMake (IMAGE_X,-30, x, x); imageView. alpha = IMAGE_ALPHA; [self. view addSubview: imageView]; [_ imagesArray addObject: imageView];} // create a clock and add it to the master loop. CADisplayLink * link = [CADisplayLink displayLinkWithTarget: self selector: @ selector (makeSnow)]; [link addToRunLoop: [nsunloop mainRunLoop] forMode: NSDefaultRunLoopMode];}-(void) makeSnow {if (_ imagesArray. count> 0) {UIImageView * imageView = _ imagesArray [0]; [_ imagesArray removeObjectAtIndex: 0]; [self snowFall: imageView];}-(void) snowFall :( UIImageView *) imageView {[UIView animateWithDuration: 10 animations: ^ {imageView. frame = CGRectMake (imageView. frame. origin. x, Main_Screen_Height, imageView. frame. size. width, imageView. frame. size. height); imageView. transform = CGAffineTransformMakeScale (0.3, 0.3); imageView. transform = CGAffineTransformRotate (imageView. transform, M_PI);} completion: ^ (BOOL finished) {float x = IMAGE_WIDTH; imageView. frame = CGRectMake (IMAGE_X,-30, x, x); [_ imagesArray addObject: imageView] ;}];}
4. There must be no fewer demos.
Https://github.com/Esdeath/snow/tree/master/SnowFlyTwo
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.