2012-05-02 27 views
0

カードのアニメーションをアニメーションしようとしています。現在私はディーラーカード(dCard1)とプレーヤーカード(pCard1)という2枚のカードを持っています。別の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:animations:completion:versionを使用して連鎖します。最初のアニメーションの完了:ブロックに2番目のアニメーションを指定できます。

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

関連する問題