I have over 200 images that I want to animate in about 10 seconds. I tried using animationImages by loading the images into an array and then calling the startAnimating method. This worked well in the simulator, but crashed the iPad.
I tried calling NStimer every 1/25 of a second and changing the image every time the timer fires. It was better than the previous method, it worked well in the simulator, but also crashed at the end (lags) of the animation on the iPad.
Can someone help me and tell me the perfect way to approach this issue? Thanks.
ORIGINAL CODE:
- (void) humptyFallingAnim { NSString *filename; if (humptyImageCounter < 285) { filename = [NSString stringWithFormat:@"Humpty Animation HD1.2 png sequence/humpty_HD1.2_%d.png", humptyImageCounter]; UIImage *someImage = [UIImage imageNamed:filename]; humptyFalling.image = someImage; NSLog(@"loaded image: %d", humptyImageCounter); humptyImageCounter++; } else { NSLog(@"Timer invalidated"); [humptyTimer invalidate]; humptyTimer = nil; } }
EDIT: some new code that doesn't work for me
NSString *filename; if (humptyImageCounter < 285) { filename = [NSString stringWithFormat:@"Humpty Animation HD1.2 png sequence/humpty_HD1.2_%d.png", humptyImageCounter]; @autoreleasepool { UIImage *someImage = [UIImage imageWithContentsOfFile:filename]; humptyFalling.image = someImage; NSLog(@"loaded image: %d", humptyImageCounter); } humptyImageCounter++; } else { NSLog(@"Timer invalidated"); [humptyTimer invalidate]; humptyTimer = nil; }
EDIT 2:
-(void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { NSLog(@"You shook it!"); [self.view bringSubviewToFront:imgSnail]; if (deviceHasBeenShaken == 0) { deviceHasBeenShaken = 1; } humptyTimer = [NSTimer scheduledTimerWithTimeInterval:(1/25) target:self selector:@selector(humptyFallingAnim) userInfo:nil repeats:YES]; [self moveHumptyPosition]; }
source share