uiimageView动画的问题:动画停止

发布于 2024-11-26 03:17:13 字数 503 浏览 0 评论 0原文

这是我的代码:

-(void) createNewBall {

  UIImage * image = [UIImage imageNamed:@"bulle_03.png"];
  bulleBouge = [[UIImageView alloc] initWithImage:image];

  [bulleBouge setCenter:[self randomPointSquare]];
  [[self view] addSubview:bulleBouge];

}

-(void)moveTheBall{

  bulleBouge.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y);

}

每两秒调用一次createNewBall。我的问题是,创建的每个 bulleBouge 都会在两秒后停止移动。我不知道为什么。

请问我该如何解决这个问题?

Here is my code:

-(void) createNewBall {

  UIImage * image = [UIImage imageNamed:@"bulle_03.png"];
  bulleBouge = [[UIImageView alloc] initWithImage:image];

  [bulleBouge setCenter:[self randomPointSquare]];
  [[self view] addSubview:bulleBouge];

}

-(void)moveTheBall{

  bulleBouge.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y);

}

createNewBall is called every two seconds. My problem is that every bulleBouge that is created stops moving after two seconds. I don't know why.

How can I solve this please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸢与 2024-12-03 03:17:13

它停止移动 b/cu 每两秒初始化一次新的 bulleBouge。您还会泄漏内存,因为在为其分配新值之前从未释放它。所以发生的情况是,在创建 imageView 之后,您只保留对最后一个实例的引用,因此只有最后一个实例正在改变位置。要解决此问题,请将所有新的 uiImageView 存储在一个数组中,并在两秒后随机移动它们。

-(void) createNewBall {

  UIImage * image = [UIImage imageNamed:@"bulle_03.png"];
  UIImageView *bulleBouge = [[UIImageView alloc] initWithImage:image];
  [bulleBouge setCenter:[self randomPointSquare]];

  [bulleBougeArray addObject:bulleBouge];

  [[self view] addSubview:bulleBouge];

  [bulleBouge release];

}

-(void)moveTheBall{

  for(int i=0; i< [bulleBougeArray count];i++){
    UIImageView *bulleBouge = [bulleBougeArray objectAtIndex:i];
    bulleBouge.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y);
   }

}

It stops moving b/c u are initializing new bulleBouge every two seconds. You are also leakin memory since you never releasy it before assigning new value to it. so what happens is that after u create the imageView you only keep the reference to the lasts instance, hence only the last one is changing position. To fix this store all your new uiImageViews in an array and move them randomly after two seconds.

-(void) createNewBall {

  UIImage * image = [UIImage imageNamed:@"bulle_03.png"];
  UIImageView *bulleBouge = [[UIImageView alloc] initWithImage:image];
  [bulleBouge setCenter:[self randomPointSquare]];

  [bulleBougeArray addObject:bulleBouge];

  [[self view] addSubview:bulleBouge];

  [bulleBouge release];

}

-(void)moveTheBall{

  for(int i=0; i< [bulleBougeArray count];i++){
    UIImageView *bulleBouge = [bulleBougeArray objectAtIndex:i];
    bulleBouge.center = CGPointMake(imageView.center.x + X, imageView.center.y + Y);
   }

}
甜是你 2024-12-03 03:17:13

西普里安是正确的。在更简单的形式中,您每两秒在同一变量下创建一个新的 bulleBouge。该程序已经有一个,但由于您告诉它在同一 ivar 下创建一个新的,它会忘记旧的,因此不会移动它。您需要一个数组,以便可以单独记住每个球,从而单独移动,如他发布的示例代码所示。

Cyprian is correct. In a simpler form, you're creating a new bulleBouge every two seconds, under the same variable. The program already has one, but since you told it to make a new one under the same ivar it forgets the old one, and thus doesn't move it. You need an array so that each ball can be remembered separately, and thus moved separately as seen in the example code that he posted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文