我在使用滑块触发一系列动画时遇到问题,这是代码:
-(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
块可以正常运行,但是每次调用completion
时nextPhotoAnimation
块都会运行几次。当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]);
}
}
];
}