无法在UIImageView中加载2个单独的图像序列

时间:2011-07-05 20:39:16

标签: iphone ios xcode ipad

我不知道这是否可行,但是我需要在imageView中加载一个图像序列并让它只播放一次,然后卸载该序列并将第二个图像序列加载到同一个imageView控件中。

这是我用来加载和播放第一个序列的代码。

imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"openingSeq0001.jpg"], 
...etc.
[UIImage imageNamed:@"openingSeq0140.jpg"],nil];

imageView.animationDuration = 2;
imageView.animationRepeatCount = 1;
[imageView startAnimating];
[self.view addSubview:imageView];
[super viewDidLoad];

我现在想要在播放一次后将第二个序列加载到同一个imageView控件中。我尝试过if语句,甚至是while循环来确保动画至少播放一次并递减计数器1并将值传递给imageView.animationRepeatCount等于0。

imageView.animationRepeatCount = 0;

我以为我可以搞清楚,但我在某处遗漏了某些东西。我可以让第二组图像运行或第一组然后停止。我使用了do while循环。

我用它来使它工作:

SEL methodSelector = @selector(startSeqTwo);
[NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:methodSelector userInfo:nil repeats:NO];

我在第一个代码块之后使用了选择器来加载第一个序列并调用方法:

-(void)startSeqTwo;

2 个答案:

答案 0 :(得分:0)

我还没有这样做,但您可以尝试使用self performSelectorOnMainThread:(SEL) withObject:(id) waitUntilDone:(BOOL)功能。

-(void)animateSequence{
   [self performSelectorOnMainThread:@selector(animateFirstSequence) withObject:nil waitUnitilDone: YES];
   [self performSelectorOnMainThread:@selector(animateSecondSequence) withObject:nil waitUnitilDone: YES];
}

-(void)animateFirstSequence{
  imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"openingSeq0001.jpg"], 
...etc.
   [UIImage imageNamed:@"openingSeq0140.jpg"],nil];

  imageView.animationDuration = 2;
  imageView.animationRepeatCount = 1;
  [imageView startAnimating];
}

-(void)animateSecondSequence{
  imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"openingSeq0001.jpg"], 
...etc.
   [UIImage imageNamed:@"openingSeq0140.jpg"],nil];

  imageView.animationDuration = 2;
  imageView.animationRepeatCount = 1;
  [imageView startAnimating];
}

修改

您还可以尝试检查UIImageView是否是stil动画,并等到完成播放另一个序列。你会这样做:

-(void)animateSequence{
  //First sequence
  imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"openingSeq0001.jpg"], 
...etc.
   [UIImage imageNamed:@"openingSeq0140.jpg"],nil];

  imageView.animationDuration = 2;
  imageView.animationRepeatCount = 1;
  [imageView startAnimating];

  while([imageView isAnimating])
      //Do nothing

//second sequence
imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"openingSeq0001.jpg"], 
...etc.
   [UIImage imageNamed:@"openingSeq0140.jpg"],nil];

  imageView.animationDuration = 2;
  imageView.animationRepeatCount = 1;
  [imageView startAnimating];
}

答案 1 :(得分:0)

SEL methodSelector = @selector(startSeqTwo);
[NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:methodSelector userInfo:nil repeats:NO];

这很有用。

我在第一个代码块之后使用了选择器来加载第一个序列并调用了方法: - (void)startSeqTwo;