The effect is as follows:
ViewController.h
1 #import <UIKit/UIKit.h>23@interface viewcontroller: Uiviewcontroller4 @property (Strong, Nonatomic) UIImage *Imgsnowflake; 5 6 @end
Viewcontroller.m
1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4- (void) Layoutui;5- (void) Snow;6 @end7 8 @implementationViewcontroller9 #defineKapplicationframe [[UIScreen mainscreen] applicationframe]Ten #defineKwidthofsnowflake 30.0 One A- (void) Viewdidload { - [Super Viewdidload]; - the [self layoutui]; - } - -- (void) didreceivememorywarning { + [Super didreceivememorywarning]; - //Dispose of any resources the can be recreated. + } A at- (void) Layoutui { -Self.view.backgroundColor = [Uicolor colorwithred:0.218Green0.219Blue0.196Alpha1.000]; -_imgsnowflake = [UIImage imagenamed:@"Snowflake"]; - - //timer, executed every 0.5 seconds -[Nstimer Scheduledtimerwithtimeinterval:0.5 in target:self - selector: @selector (Snow) to Userinfo:nil + Repeats:yes]; - } the *- (void) Snow { $NSString *strwidthofscene = [NSString stringWithFormat:@"%f", kapplicationframe.size.width-Kwidthofsnowflake];Panax NotoginsengCGFloat StartX = Arc4random ()%[strwidthofscene IntegerValue];//generate random number 0 to strWidthOfScene-1 -CGFloat endx = arc4random ()% ([Strwidthofscene integervalue]+1);//generate random number 1 to Strwidthofscene the +Uiimageview *IMGV =[[Uiimageview alloc] initwithimage:_imgsnowflake]; AImgv.frame = CGRectMake (StartX,-20.0, Kwidthofsnowflake, kwidthofsnowflake); theImgv.alpha =0.8; + [Self.view ADDSUBVIEW:IMGV]; - $ [UIView Beginanimations:nil context:null]; $[UIView setanimationduration:5]; -Imgv.frame =CGRectMake (EndX, -kapplicationframe.size.height+20.0-Kwidthofsnowflake, the Kwidthofsnowflake, - kwidthofsnowflake);Wuyi [UIView commitanimations]; the } - Wu @end
178 achieve snow-flying effect