проблема с несколькими анимациями одновременно - PullRequest
0 голосов
/ 26 июля 2011

вот мой код:

-(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 Ответ

1 голос
/ 26 июля 2011

Вместо этого сохраняйте ссылку на точку, в которой вы хотите, чтобы все ваши изображения тоже двигались. Использование 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);
   }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...