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!

这个想法是,您可以“玩”子视图,因为您可以将它们的 alpha 属性从 0.0(不可见)更改为 1.0(完全不透明),这由 SDK 的视图动画支持。

请根据您自己的文件名更改图像名称(我在这里使用了自己的)。

以下方法检查按钮的图像是否是应该调用动画的图像 - 正是您之前所做的。如果满足此条件,它会在视觉上将按钮图像更改为另一个图像:

- (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 回答