2012-05-02 5 views
0

Я пытаюсь анимировать работу карт. В настоящее время у меня две карты, карта дилера (dCard1) и карта игрока (pCard1).Сделать UIImageView анимировать после другого UIImageView завершен

Карту игрока следует сдавать, и как только он будет сдан, раздаточная карта должна быть сдана.

В настоящее время карта игрока будет анимировать правильно, когда я нажимаю кнопку сделки, но вместо того, чтобы дождаться, когда карта игрока будет завершена, карта дилера появится только у дилера. Он не оживляет. Я новичок в создании анимаций, любая помощь будет очень признательна.

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

}

ответ

0

У меня нет доступа к вашим классам (конечно), но я думаю, что это очень близко к «вставных готовый» код. Как вы думаете?

- (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]; 
    } 
} 
+0

Я не могу получить изображения, чтобы показать их, это определенно выглядит как лучший способ обойти его, чем я ранее пытался. Любая подсказка, почему изображения не будут отображаться? –

+0

Возможно, вам захочется разделить некоторые строки кода, чтобы вы могли перейти в режим отладки и убедиться, что у вас есть допустимые значения. – mbm29414

+0

Я просто не добавлял UIImageView в качестве подсмотра. Я изменил ваш код еще немного, чтобы соответствовать моим потребностям, но все работает нормально. благодаря –

1

Если вы просто анимировать кадр зрения, вы можете использовать более простой UIView animateWithDuration ... методы.

Вы связываете их с помощью animateWithDuration: анимации: завершение: версия. Вы можете указать вторую анимацию в блоке завершения: блок первой анимации.

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