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