多个动画同时出现的问题

时间:2011-07-26 17:15:52

标签: iphone arrays xcode animation uiimageview

这是我的代码:

-(void) createNewImage {
UIImage * image = [UIImage imageNamed:@"abouffer_03.png"];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setCenter:[self randomPointSquare]];
 [imageViewArray addObject:imageView];
[[self view] addSubview:imageView];
[imageView release];
}

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

-(void)viewDidLoad {
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(onTimer2)];
[displayLink setFrameInterval:1];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
imageViewArray = [[NSMutableArray alloc]init];

}

所以我想做的是http://www.youtube.com/watch?v=rD3MTTPaK98。 但我的问题是,在创建了imageView(createNewImage)后,它会在4秒后停止(可能是由于计时器)。我希望imageView在创建新的imageView时继续移动。我该怎么办?对不起我的英语我是法国人:/

1 个答案:

答案 0 :(得分:1)

相反,请保留对您希望所有图像也移动的点的引用。使用NSTimer并在计时器中自己移动所有图像最终会减慢你的应用程序(我从经验中知道)。使用UIView动画块,并告诉它在创建时移动到该点。

-(void) createNewImage {
   UIImage * image = [UIImage imageNamed:@"abouffer_03.png"];
   imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
   [imageView setCenter:[self randomPointSquare]];

   //Move to the centerPoint
   [self moveTheImage:imageView];

   [imageViewArray addObject:imageView];
   [[self view] addSubview:imageView];
}

-(void)moveTheImage:(UIImageView *)imageView {
   [UIView animateWithDuration:1.0
                    animations:^{
                       [imageView setCenter:centerPoint];
                    }];
}

-(void)viewDidLoad {
   [super viewDidLoad];
   imageViewArray = [[NSMutableArray alloc]init];

   //IDK what all that other code was

   centerPoint = self.view.center;
}

编辑:在动画期间查找UIImage

您需要引用UIImageView的表示层以在动画期间找到它的位置

UIImageView *image = [imageViewArray objectAtIndex:0];
CGRect currentFrame = [[[image layer] presentationLayer] frame];

for(UIImageView *otherImage in imageViewArray) {

   CGRect objectFrame = [[[otherImage layer] presentationLayer] frame];

   if(CGRectIntersectsRect(currentFrame, objectFrame)) {
       NSLog(@"OMG, image: %@ intersects object: %@", image, otherImage);
   }
}