2

こんにちは皆さん、私は xcode を初めて使用し、画像を変更してタッチでバルーンをアニメーション化しようとしています: これは私のコードです: 今私が直面している問題は、画像がアニメーション化されていないということは、アニメーション タイマーが機能していないことを意味します: 何をすべきかを教えてください私は時間をかけて画像をアニメーション化します。適切に行っていない場合は、NSTimerでどのように行うことができるか教えてください。

-(void)baloonbursting:(UIButton *)button withEvent:(UIEvent *)event{
if ([[UIImage imageNamed:@"redbaloons.png"] isEqual:button.currentImage]) {
    NSLog(@"em redbaloons.png");
    UIImage *bubbleImage3 = [UIImage imageNamed:@"redburst.png"];
    [button setImage:bubbleImage3 forState:UIControlStateNormal];
}
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView animateWithDuration:1.0f animations:^(){
    // define animation
    if ([[UIImage imageNamed:@"redburst.png"] isEqual:button.currentImage]) {
        NSLog(@"em redbaloons.png");
        UIImage *bubbleImage3 = [UIImage imageNamed:@"redburst2.png"];
        [button setImage:bubbleImage3 forState:UIControlStateNormal];
    }   
}
 completion:^(BOOL finished){
 // after the animation is completed call showAnimation again
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionAllowUserInteraction animations:^{

                     } completion:^(BOOL finished){
                         if (finished) {
                             [button removeFromSuperview];
                         }}];
                 }];

}

4

1 に答える 1

2

考える正しい方向性を示す解決策を教えてほしい。つまり、Xcode で小さなテスト プロジェクトを開発し、このコードが実際に機能することを確認した理由です。

最初: このアニメーションの NSTimers は忘れてください!

SDK のビュー アニメーションでサポートされているアルファ プロパティを 0.0 (不可視) から 1.0 (完全に不透明) に変更できるため、サブビューを「いじる」という考え方です。

独自のファイル名に従って画像名を変更してください (ここでは独自のものを使用しました)。

次のメソッドは、ボタンの画像がアニメーションを呼び出すものであるかどうかをチェックします - まさにあなたが以前にしたことです。この条件が満たされると、ボタンの画像が別の画像に変化する様子が視覚的にアニメーション化されます。

- (IBAction)balloonBursting:(UIButton *)sender
{
    BOOL doAnimate = NO;

    UIImageView *ivOldBubbleImage;
    UIImageView *ivNewBubbleImage;

    if ([[UIImage imageNamed:@"BalloonYellow.png"] isEqual:sender.currentImage]) {
        NSLog(@"will animate");
        doAnimate = YES;

        UIImage *newImage = [UIImage imageNamed:@"BalloonPurple.png"];
        ivNewBubbleImage = [[UIImageView alloc] initWithImage:newImage];
        ivNewBubbleImage.alpha = 0.0;
        ivOldBubbleImage = sender.imageView;
        [sender addSubview:ivNewBubbleImage];
    }

    if (doAnimate) {
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView animateWithDuration:1.0f animations:^(){
            // define animation
            ivOldBubbleImage.alpha = 0.0;
            ivNewBubbleImage.alpha = 1.0;
        }
                         completion:^(BOOL finished){
                             [sender setImage:ivNewBubbleImage.image forState:UIControlStateNormal];
                             [ivNewBubbleImage removeFromSuperview];
                         }];
    }
}
于 2013-07-11T12:47:10.297 に答える