我在使用滑块触发一系列动画时遇到问题,这是代码:

-(void)slideAlpha:(id)sender{

  self.bigPhotoViewA.alpha = self.alphaSlider.value;

  if (self.alphaSlider.value == 1){

    [UIView animateWithDuration:1
                     animations:^{
                         self.alphaSlider.alpha = 0;
                     } completion:nil
    ];

    [self performSelector:@selector(nextPhotoAnimation) withObject:self afterDelay:5.0 ];
  }
}

-(void) nextPhotoAnimation{

  self.alphaSlider.value = 0;

  [UIView animateWithDuration:2
                   animations:^{
                       self.bigPhotoViewA.alpha = 0.0;
                       self.bigPhotoView.alpha = 0.0;
                       self.smallPhotoView.center = CGPointMake(startX, startY);
                   }
                   completion:^(BOOL finished) {
                       NSLog(@"Animation ended");
                       self.smallPhotoView.image = ((UIImage *)[smallImagesArray objectAtIndex:imageCount]);
                   }
  ];
}


因此,当滑块达到1值时,在延迟后启动nextPhotoAnimation。到目前为止,一切都很好。问题出在nextPhotoAnimation内部。 animations块可以正常运行,但是每次调用completionnextPhotoAnimation块都会运行几次。当NSLog启动时,我得到显示的6至9次nextPhotoAnimation,然后在2秒后的正确时间再次得到它。



我试图用更简单的代码来复制问题,并且animation / completion流工作正常。

最佳答案

nextPhotoAnimation中尝试一下,

-(void) nextPhotoAnimation{

    self.alphaSlider.value = 0;

    [UIView animateWithDuration:2
               animations:^{
                   self.bigPhotoViewA.alpha = 0.0;
                   self.bigPhotoView.alpha = 0.0;
                   self.smallPhotoView.center = CGPointMake(startX, startY);
               }
               completion:^(BOOL finished) {
                   if (finished) {
                       NSLog(@"Animation ended");
                       self.smallPhotoView.image = ((UIImage *)[smallImagesArray objectAtIndex:imageCount]);
                   }
               }
    ];
}

10-08 03:22