0

这听起来可能真的很菜鸟,但我花了一整天的时间来解决这个问题,希望能得到一些帮助。

你看我有一个我在游戏中不止一次调用的方法。如果我使用[self myMethod];它,它可以工作一次。然后当我再次调用它时,方法中的动画不再开始。

我需要的是用可以“分配”和“释放”的替代方法替换“自我”,以使我的动画正常工作。

我试过了;

@implementation gameViewController


gameViewController *object = [[gameViewController alloc] init];

[object myMethod];

然而,上面对 self 的替代甚至没有调用该方法。我不知道我做错了什么,它应该像“自我”一样工作。

有什么我想念的吗?你如何让类的对象像“self”一样工作?

非常感谢。

这是我的代码的更详细的外观;

[自爆动画];

- (void) explosionAnimations
{


      UIImage *image = [UIImage imageNamed: @"Yellow Explosion.png"];

      [bomb setImage:image];   

      [UIView beginAnimations:@"bomb1ExplosionIncrease" context:NULL];

      [UIView setAnimationDuration:0.5];

      [UIView setAnimationDelegate:self];

      bomb.transform = CGAffineTransformMakeScale(3.4, 3.4);

      [UIView commitAnimations];

}

setImage 每次都可以正常工作。但是动画在该方法的第二次调用中停止工作。在控制台中,它会记录“动画完成”,而图像没有发生任何事情。

这使我相信“​​自我”以某种方式相信动画已经完成,并且不会再费心去做。所以我认为一个新的“alloc”可能会让它清醒。

4

1 回答 1

5

这个问题与“自我”没有任何关系。问题是你在动画中设置了变换,然后当你再次运行它时,你设置了相同的变换,所以它什么也不做。您需要将帧重置为新帧,然后在再次执行动画之前将变换设置回身份变换。此外,您应该使用基于块的动画。我不确定这是不是最好的方法,但这对我有用(如果你关闭了自动布局)。

- (void)explosionAnimations {

    UIImage *image = [UIImage imageNamed: @"Yellow Explosion.png"];
    [self.bomb setImage:image];

    [UIView animateWithDuration:.5 animations:^{
        self.bomb.transform = CGAffineTransformMakeScale(3.4, 3.4);
    } completion:^(BOOL finished) {
        CGRect newFrame = self.bomb.frame;
        self.bomb.transform = CGAffineTransformIdentity;
        self.bomb.frame = newFrame;
    }];
}

如果您在启用了自动布局(默认情况下)的应用程序中执行此操作,那么我不会使用转换,而只是通过调整其高度和宽度约束来调整图像视图的宽度和高度。因此,在这种方法中,您应该将 IBOutlets 设置为您在 IB 中设置的高度和宽度约束,然后在动画块中更改它们的常量值:

[UIView animateWithDuration:.5 animations:^{
        self.heightCon.constant = self.heightCon.constant * 3.4;
        self.widthCon.constant = self.widthCon.constant * 3.4;
        [self.view layoutIfNeeded];
    }];
于 2013-07-27T16:16:43.337 回答