嗨,这里的每一个都是我的代码:
-(void) createNewImage {
[imageView setCenter:[self randomPointSquare]];
[[self view] addSubview:imageView];
[views addObject:imageView];
[imageView release];
ix=imageView.center.x;
iy=imageView.center.y;
[XArray addObject:[NSNumber numberWithFloat:(240 - ix)/diviseurVitesse]];
[YArray addObject:[NSNumber numberWithFloat:(160 - iy)/diviseurVitesse]];
}
-(void)moveTheImage{
for (NSUInteger i = 0; i < [views count]; i++) {
imageView = [views objectAtIndex:i];
X = [[XArray objectAtIndex:i] floatValue];
Y = [[YArray objectAtIndex:i] floatValue];
imageView.center=CGPointMake(imageView.center.x + X, imageView.center.y + Y);
} 使用此代码,图像被创建并移动到屏幕的中心,但在几秒钟后性能下降并达到30fps。我想将fps保持在60但我不知道如何继续。有些人说我可以使用游泳池,但我很难使用它,因为我真的不知道如何使用它:/对不起我的英语我是法国人:/
答案 0 :(得分:0)
您的问题很可能就在这里 - [views addObject:imageView];
。将对象添加到像NSMutableArray这样的持久数据集会保留对象,因此内存消耗实际上会增长。尝试在[self.view subviews]
中操作图像,而不将它们存储在数组中。
编辑:您的代码看起来应该是这样的
- (void)createAndMove {
UIImageView *imageView = [UIImageView alloc] initWithImage:image];
[imageView setCenter:[self randomPointSquare]];
[self.view addSubview:imageView];
[UIView animateWithDuration:5 animations:^{
imageView.center = self.view.center;
}];
}
编辑2:
- (void)moveImages {
for (UIImageView *img in [self.view subviews]) {
[UIView animateWithDuration:5 animations:^{
img.center = self.view.center;
}];
}
}
答案 1 :(得分:0)
当物体到达屏幕中心时会发生什么?如果它只是坐在那里而其他图像与它重叠,那么您可以将其重置为处于起始位置而不是创建新图像。如果你将它们移动到中心然后将它们留在那里,很快就会有很多视图,这将会破坏性能。
所以,假设你有3个观点。视图1已创建并出现在屏幕边缘并向中心移动。当它位于中心的一半时,会创建视图2并显示在屏幕边缘并开始向中心移动。当视图2位于中心的一半时,视图1位于中心,并且创建了视图3并显示在屏幕的边缘。现在,当视图2到达中心时,视图1被重置为位于屏幕的边缘(它将被视图2覆盖,因此不会被注意到)。然后当视图3到达中心时,它将覆盖视图2,以便可以重置到屏幕边缘。
希望这是有道理的 - 如果没有绘制图片或确切知道你在屏幕上移动的内容等等,这有点困难。