[英]Problem with uiimageView animation: animation stops
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. createNewBall
每两秒钟调用一次。 My problem is that every bulleBouge
that is created stops moving after two seconds. 我的问题是,创建的每个
bulleBouge
都会在两秒钟后停止移动。 I don't know why. 我不知道为什么
How can I solve this please? 请问该如何解决?
It stops moving b/cu are initializing new bulleBouge every two seconds. 它停止移动b / cu每两秒钟初始化一次新的bulleBouge。 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.
所以发生的事情是,在创建imageView之后,您仅保留对lasts实例的引用,因此只有最后一个实例正在更改位置。 To fix this store all your new uiImageViews in an array and move them randomly after two seconds.
要修复此问题,请将所有新的uiImageViews存储在一个数组中,并在两秒钟后随机移动它们。
-(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);
}
}
Cyprian is correct. 塞浦路斯人是正确的。 In a simpler form, you're creating a new bulleBouge every two seconds, under the same variable.
以一种更简单的形式,您每两秒钟在相同的变量下创建一个新的bulleBouge。 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.
该程序已经有一个,但是由于您告诉它要在同一个ivar下创建一个新程序,因此它会忘记旧的程序,因此不会移动它。 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.
您需要一个数组,以便可以分别记住每个球,并分别移动,如他发布的示例代码所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.