【问题标题】:Make UIImageView animate after another UIImageView has finished在另一个 UIImageView 完成后使 UIImageView 动画
【发布时间】:2012-05-02 18:22:39
【问题描述】:

我正在尝试动画处理纸牌。目前我有两张牌,庄家牌(dCard1)和玩家牌(pCard1)。

应该发玩家牌,一旦发牌,就应该发庄家牌。

当前发生的情况是,当我按下发牌按钮时,玩家卡片会正确动画,但不是等到玩家卡片完成动画,而是庄家卡片出现在dealerTo 位置。它没有动画。我是做动画的新手,任何帮助将不胜感激。

-(void) animateHandsDealt: (Hand*) pHand:(Hand*) dHand{
pCard1= [[UIImageView alloc] initWithFrame:CGRectMake(125, 0, 75, 110)];
dCard1= [[UIImageView alloc] initWithFrame:CGRectMake(125, 0, 75, 110)];

CGFloat start = 0;
CGFloat duration = 1;

CGPoint playerTo = CGPointMake(120, 300);
CGPoint dealerTo = CGPointMake(120, 70);

pCard1.image = [UIImage imageNamed:[[pHand.cardArr objectAtIndex:0 ] imagePath]];
[self.view addSubview: pCard1];
[playerImages addObject:pCard1];

CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position" ];
move.fillMode = kCAFillModeBoth;
move.beginTime = start;
[move setToValue:[NSValue valueWithCGPoint:playerTo]];
[move setDuration:duration];
move.removedOnCompletion = FALSE;
[[pCard1 layer] addAnimation:move forKey:@"position"];


dCard1.image = [UIImage imageNamed:@"back-red-75-1.png"];
[self.view addSubview: dCard1];
CABasicAnimation *move2 = [CABasicAnimation animationWithKeyPath:@"position" ];
[move2 setToValue:[NSValue valueWithCGPoint:dealerTo]];
move2.beginTime = start + duration;
[move2 setDuration:duration];
move2.fillMode = kCAFillModeBoth;
move2.removedOnCompletion = FALSE;
[[dCard1 layer] addAnimation:move2 forKey:@"position"];

}

【问题讨论】:

    标签: ios core-animation cabasicanimation


    【解决方案1】:

    如果您只是为视图的框架设置动画,则可以使用更简单的 UIView animateWithDuration... 方法。

    您可以使用 animateWithDuration:animations:completion: 版本链接它们。可以在第一个动画的completion:块中指定第二个动画。

    http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/doc/uid/TP40006816-CH3-SW109

    【讨论】:

      【解决方案2】:

      我无权访问您的课程(当然),但我认为这非常接近“即插即用”代码。你怎么看?

      - (void)deal {
          // Of course, you'd replace these NSString objects with actual card objects...
          NSArray *array = [NSArray arrayWithObjects:@"pCard1", @"dCard1", @"pCard2", @"dCard2", @"pCard3", @"dCard3", nil];
          [self performSelectorOnMainThread:@selector(animateDealWithArray:) withObject:array waitUntilDone:YES];
      }
      - (void)animateDealWithArray:(NSArray *)array {
          // constants that won't change call to call
          static CGFloat duration = 1;
          static CGSize  cardSize = {75,  110};
          static CGPoint playerTo = {120, 300};
          static CGPoint dealerTo = {120, 70};
      
          // Seems like you want the card to start at the top of the "deck" whether it's a player card or not
          UIImageView *card = [[UIImageView alloc] initWithFrame:CGRectMake(125, 0, 75, 110)];
          // Figure out if the first object (the one we're currently using) is a player or dealer card
          if ([[array objectAtIndex:0] isKindOfClass:[PlayerCard Class]]) {
              // Player card, so show the "face"
              [card setImage:[UIImage imageNamed:[(PlayerCard *)[array objectAtIndex:0] imagePath]]];
              [UIView animateWithDuration:duration
                               animations:^{
                                   [card setFrame:CGRectMake(playerTo.x, playerTo.y, cardSize.width, cardSize.height)];
                               }
                               completion:^(BOOL finished) {
                                   [self dealNext:array];
                               }
               ];
          } else {
              // Dealer card, so show the back
              [card setImage:[UIImage imageNamed:@"back-red-75-1.png"]];
              [UIView animateWithDuration:duration
                               animations:^{
                                   [card setFrame:CGRectMake(dealerTo.x, dealerTo.y, cardSize.width, cardSize.height)];
                               }
                               completion:^(BOOL finished) {
                                   [self dealNext:array];
                               }
               ];
          }
      }
      - (void)dealNext:(NSArray *)array {
          int count = [array count];
          if (count > 1) {
              NSArray *newArray = [array subarrayWithRange:NSMakeRange(1, count - 1)];
              [self animateDealWithArray:newArray];
          }
      }
      

      【讨论】:

      • 我无法显示图像,这看起来确实比我之前尝试的更好。任何线索为什么图像不会显示?
      • 您可能想要拆分一些代码行,以便您可以在调试模式下单步执行并确保您获得了有效值。
      • 我只是从未将 UIImageView 添加为子视图。我修改了你的代码只是为了满足我的需要,但一切似乎都很好。谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多