The effect of a snowflake landing with a timer combined with a UIView animation

Source: Internet
Author: User

1 #import "HUAppDelegate.h"2 3 @implementationhuappdelegate4 5-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions6 {7Self.window =[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds];8Self.window.backgroundColor =[Uicolor Whitecolor];9 [Self.window makekeyandvisible];Ten      OneUiimageview *BG =[[Uiimageview alloc] initwithframe:[[uiscreen mainscreen] bounds]; ABg.image = [UIImage imagenamed:@"xueshan.jpg"]; - [Self.window ADDSUBVIEW:BG]; -      the_snowarray =[[Nsmutablearray alloc] init]; -Uiimageview *snow =Nil; -      for(inti =0; I < -; i + +) -     { +Snow = [[Uiimageview alloc] Initwithframe:cgrectmake (arc4random ()% the, - -, -, -)]; -Snow.image = [UIImage imagenamed:@"Flake.png"]; +Snow.tag =0; A [Self.window Addsubview:snow]; at [_snowarray Addobject:snow]; -     } -      -[Nstimer Scheduledtimerwithtimeinterval:0.1target:self selector: @selector (Findsnow) Userinfo:nil Repeats:yes]; -      -      in     returnYES; - } to  + //Looking for snowflakes -- (void) Findsnow the { *Uiimageview *asnow =Nil; $      for(inti =0; i < _snowarray.count; i + +)Panax Notoginseng     { -Asnow =[_snowarray objectatindex:i]; the         if(Asnow.tag = =0) +         { AAsnow.tag =1; the //[self snowdown:asnow]; //If you put this code here, then else and subsequent code can not, because there will not be allowed to animate the snowflake is called to perform an animation once +              Break; -         } $         Else $         { -Asnow = nil;//when looking for snow is not a snowflake is eligible, but at this time Asnow still have a value, if not let it point to empty time, the last snowflake (50th flower) was used once again, at this time this many snowflakes fall animation has not ended, Re-use can cause unexpected results (a snow flower will fly upwards). This bug tortured me a day, and finally the teacher helped solve the problem, thinking into a misunderstanding, is really hairenbujian.  -         } the     } - Wuyi[Self snowdown:asnow];//Call the Snowflake animation method to instantly animate the found snowflake the } -  Wu //let a snowflake animation -- (void) Snowdown: (Uiimageview *) Asnow About { $ [UIView Beginanimations:nil Context:asnow]; -[UIView setanimationduration:7]; -Asnow.frame = CGRectMake (arc4random ()% the,480, -, -); - [UIView setanimationdelegate:self]; A [UIView setanimationdidstopselector: @selector (restart:finished:context:)]; + [UIView commitanimations]; the } -  $- (void) Restart: (NSString *) Animationid finished: (NSNumber *) finished context: (void*) Asnow the { theUiimageview * Snow = (Uiimageview *) Asnow; theSnow.frame = CGRectMake (arc4random ()% the, - -, -, -); theSnow.tag =0; -}//let the animation that has fallen to the bottom reposition to the top, achieve the reuse effect, and reset the tag value, at which point the snowflakes can be re-found, falling again animation in  the /* the Note the time interval (0.14) of the timer above and the time it takes for the snowflake to drop the animation (7), and the length of the array ( About 0.14 * = 7 the this time to go a little ingenious, this is a critical value, originally I was in line with the principle of not wasting, so that every snowflake can not find and then into the animation, and then appear the above bug, if the time interval is smaller (snowflake animation will appear fault, the last snowflake animation the if the array length is reduced to 10, only more than 9 snowflakes are animated, as in the same principle, if the time + It can solve the problem, because at this time before the last snowflake found, there are already snowflakes again in the cycle of animation.  -  */ the Bayi @end

The use of timers and UIView animation produced by the animation effect is not stiff, and then can be reused for snowflake objects, saving memory, is a better way.

Here, animation Learning has ended a phase, in the use of the process needs to be combined with the actual choice.

The effect of a snowflake landing with a timer combined with a UIView animation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.