UIImageView在第3次点击之前响应部分touchesBegan代码。为什么?

时间:2009-12-20 06:24:06

标签: iphone xcode uiimageview

在模拟时,我加载了包含52个UIImageView的UIView;动画开始前至少需要3次点击。在前2次单击时,将调用touchesBegan。我知道因为NSLog(@“for before for loop”)显示在控制台上。但是控制台声明cardAnimationArray在前2次点击中是空的。

仅在第3次点击后,动画才会开始。奇怪的是,通过在第三次点击后点击其他卡片(UIImageView),动画从第一次点击开始(表现正常)。

不知何故,它只是在前2次点击中没有进入for循环!为什么这样呢???

我的代码如下:

     - (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];



}

1 个答案:

答案 0 :(得分:1)

你似乎永远不会给'k'赋值,所以它永远不会少于20。