ios - 如何在uiview动画中使用数组

标签 ios objective-c

我创建了一个 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];
 }

预期输出是:我想一个接一个地移动,而不是全部移动。

screenshot

最佳答案

必须等到当前动画播放完毕,才能开始下一个动画。一个简单的解决方案是使用递归:

-(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/

相关文章:

ios - 以编程方式更改 UIScrollView 引用

ios - 如何访问 iOS13 之前的 UIImage 深色外观

iphone - 仅对 subview 启用 UserInteraction

ios - 在 Swift 2 中为 Game Center 保存高分

ios - 调试时应用程序不会崩溃

ios - 如何不返回 UITableviewCell

ios - 不支持的配置 : This file is set to build for a version older than the deployment target. 功能可能受限

ios - 将触摸事件处理到一个 Table Cell

javascript - 创建一个新项目 React Native 不工作

objective-c - 如何对包含嵌套 JSON 字符串的 NSSArray 进行排序?