On simulation, after i load the UIView that contains 52 UIImageView; it takes at least 3 clicks before the animation begins. On the first 2 clicks, the touchesBegan is invoked. i know that because the NSLog(@"Just before for loop); was displayed on the console. However the console states that the cardAnimationArray is empty in the first 2 clicks.
Only after 3rd click, the animation begins. The weird thing is that, by clicking other cards (UIImageView) after 3rd click, the animation begins on 1st click (behaves as normal).
Somehow it just doesn't step into the for loop in the first 2 clicks! why is this so???
MY code is as below:
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:@"Cards.plist"];
NSMutableArray *array = [[NSArray alloc] initWithContentsOfFile:myPathDocs];
NSMutableDictionary *dict = [array objectAtIndex:self.tag];
NSString *cardMoved = [dict objectForKey:@"Card"];
NSMutableArray *cardAnimationPreloadingArray = [[NSMutableArray alloc] initWithCapacity:20];
self.cardAnimationArray = [[NSMutableArray alloc] initWithCapacity:20];
[cardAnimationPreloadingArray release];
NSLog(@"Just before for loop");
for (int k; k < 20; k++) {
NSLog(@"counting i: %d",k);
int j = k+1;
cardMovedImageName = [NSString stringWithFormat:@"Animated %@%d.png",cardMoved,j];
NSLog(@"cardMovedImageName %@ at %d",cardMovedImageName, k);
UIImage *cardMovedImage = [UIImage imageNamed:cardMovedImageName];
[cardAnimationArray insertObject:cardMovedImage atIndex:k];
}
CGRect selfFrame = self.frame;
selfFrame.size.width = 110;
selfFrame.size.height = 164;
[self setFrame:selfFrame];
[self.superview bringSubviewToFront:self];
CGPoint pt = [[touches anyObject] locationInView:self.superview];
self.center = pt;
NSLog(@"cardAnimationArray %@", cardAnimationArray);
self.clipsToBounds = NO;
self.animationImages = [NSArray arrayWithArray:cardAnimationArray];
[self setAnimationRepeatCount:1];
self.animationDuration= 0.3;
[self startAnimating];
self.image = [UIImage imageNamed:cardMovedImageName];
}