我创建了一个 UIView 动画,我的数组包含 10 张图像。我想在按下按钮时将一个地方移动到另一个地方。下面的代码可以正常工作,但问题是所有 10 张图片都在移动。
-(IBAction)moveImageAtIndex:(int)index {
UIButton *buttonCelebrate = [redAppleArray objectAtIndex:j];
UIButton *buttonRefer = [reffButtonArray objectAtIndex:j];
currentView = buttonCelebrate;
CGRect frame = currentView.frame;
CGRect frame1 = buttonRefer.frame;
frame.origin.x = frame1.origin.x;
frame.origin.y = frame1.origin.y;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration: 2.0];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
currentView.frame = frame;
[UIView animateWithDuration:2.0 animations:^
{
[currentView setTransform:CGAffineTransformIdentity];
}completion:^(BOOL finished) {
if (finished) {
if(index != [redAppleArray count] - 1) {
[self moveImageAtIndex:index+1];
}
}
}];
[UIView commitAnimations];
}
预期输出是:我想一个接一个地移动,而不是全部移动。
最佳答案
必须等到当前动画播放完毕,才能开始下一个动画。一个简单的解决方案是使用递归:
-(void)moveImageAtIndex:(int)index {
[UIView animateWithDuration:2.0 animations:^{
//Do animation
}completion:^(BOOL finished) {
if (finished) {
if(index != [redAppleArray count] - 1) {
[self moveImageAtIndex:index + 1];
}
}
}];
}
关于ios - 如何在uiview动画中使用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20396964/