同时处理多个动画的问题

发布于 2024-11-26 11:04:32 字数 1194 浏览 1 评论 0原文

这是我的代码:

-(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 时继续移动。请问我该怎么做?抱歉我的英语我是法国人:/

here is my code :

-(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];

}

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 :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请别遗忘我 2024-12-03 11:04:33

相反,请保留对您希望所有图像也移动的点的引用。使用 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);
   }
}

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);
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文