【问题标题】:problem with multiple animation at the same time多个动画同时出现的问题
【发布时间】:2011-07-26 17:15:52
【问题描述】:

这是我的代码:

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

【问题讨论】:

  • 您的onTimer 代码在哪里?
  • 不,我用 createNEwImage 改变 onTimer 和用 moveTheImage 改变 onTimer2
  • 我在问你的 onTimer 函数中有什么代码。
  • onTimer 是 createNew Image 。这是一个错误

标签: iphone arrays xcode animation uiimageview


【解决方案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);
   }
}

【讨论】:

  • 我使用 NSTimer 因为 UIView 动画块不可能使用 CGRectIntersects 来进行碰撞,所以系统只知道起点和终点。这就是为什么我不使用 UIView 动画
  • "我使用 NSTimer,因为 UIView 动画块无法使用 CGRectIntersects 进行碰撞,因此系统只知道起点和终点。"这不是真的。您要说的是您不知道动画期间图片的位置。你使用 UIView 的表现层来计算这个位置,看我提供的更新代码。
猜你喜欢
  • 1970-01-01
  • 2020-04-21
  • 1970-01-01
  • 2021-10-13
  • 2021-09-30
  • 2020-07-04
  • 1970-01-01
  • 2020-01-10
  • 1970-01-01
相关资源
最近更新 更多