Learning exercises-Implementation of IOS snowflake falling

Source: Internet
Author: User

Learning exercises-Implementation of IOS snowflake falling

Method 1: Implement NSMutableArray

NSMutable * array; global variable, called with a timer in the. m file

-(Void) down {static int jianGe = 0; jianGe ++; if (jianGe> 2) {jianGe = 0; [self snowState];} [self snowDown];} -(void) snowState {for (int I = 0; I <[array count]; I ++) {MyImageView * img = [array objectAtIndex: I]; if (img. tag = 0) {img. tag = 1; [self. view addSubview: img]; break; // extract snowflake monolithic }}- (void) snowDown {for (int I = 0; I <[array count]; I ++) {MyImageView * img = [array objectAtIndex: I]; if (img. tag = 1) {img. center = CGPointMake (img. center. x, img. center. y + 10); if (img. center. y> 480 + 25) {img. tag = 0; img. center = CGPointMake (arc4random () % 370-50,-50 );}}}}

 

Method 2: Use Frame Animation

Call With Timer

 

// ViewController. m-(void) snowDown {// animation effect UIImageView * iv = [[UIImageView alloc] init]; int a = arc4random () % 51; // random snowflake size (0 ~ 50) iv. frame = CGRectMake (arc4random () % 370-50,-50, a, a); iv. image = [UIImage imageNamed: @ "flake"]; [self. view addSubview: iv]; [UIView beginAnimations: nil context :( _ bridge void *) (iv)]; [UIView setAnimationDuration: 5]; [UIView setAnimationDelegate: self]; [UIView setAnimationDidStopSelector: @ selector (animationDidStop: finished: context :)]; iv. frame = CGRectMake (arc4random () % 370-50,440, a, a); [UIView commitAnimations];}-(void) animationDidStop :( NSString *) animationID finished :( NSNumber *) finished context :( void *) context // eliminate {UIImageView * image = (_ bridge UIImageView *) context; [image removeFromSuperview] at the bottom;}

 

Frame Animation is simpler and easier to understand than NSMutableArray

Related Article

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.