here is my code :
-(void) createNewImage {
UIImage * image = [UIImage imageNamed:@"abouffer_03.png"];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setCenter:[self random开发者_StackOverflow中文版PointSquare]];
[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];
}
So what I want to do is http://www.youtube.com/watch?v=rD3MTTPaK98. But my problem is that after imageView is created(createNewImage) it stops after 4 seconds (maybe due to the timer).I want that imageView continue moving while new imageView are created. How can I do this please ? sorry for my english I'm french :/
Instead, keep a reference to the point you want all your images to move too. Using an NSTimer and moving all the images yourself in the timer will eventually slow down your app a lot (I know from experience). Use UIView animation blocks, and just tell it to move to the point when you create it.
-(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;
}
EDIT: Finding the UIImage during animation
You need to reference the presentation layer of the UIImageView to find its position during an animation
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);
}
}
精彩评论