如何在uiview动画中使用数组

时间:2013-12-05 10:05:48

标签: 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

3 个答案:

答案 0 :(得分:2)

您必须等到当前动画结束,然后才能开始下一个动画。一个简单的解决方案是使用递归:

-(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];
             }
         }
     }];
}

答案 1 :(得分:1)

从循环中删除代码并保留在按钮调用的方法中,然后按下按钮,阵列中的图像将根据用户点击逐个移动。

在.h文件中将i声明为int并在viewDidLoad中将i值设为0并且不要忘记在最后增加i值

使用下面的代码

-(void)viewDidLoad {
    i=0;
    [super viewDidLoad];
}
-(IBAction)methodName:(id)sender {

    UIButton *buttonCelebrate = [redAppleArray objectAtIndex:index];
    currentView = buttonCelebrate;
    CGRect frame = currentView.frame;
    CGRect frame1 = reffButton.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) {

         }
     }];

    [UIView commitAnimations];
    i++;
}

根据Eric使用下面的代码

您必须等到当前动画结束,然后才能开始下一个动画。一个简单的解决方案是使用递归:

-(void)moveImageAtIndex:(int)index {
    [UIView animateWithDuration:2.0 animations:^{
    UIButton *buttonCelebrate = [redAppleArray objectAtIndex:i];


    currentView = buttonCelebrate;

    CGRect frame = currentView.frame;
    CGRect frame1 = reffButton.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];
}

答案 2 :(得分:0)

新解决方案是:

-(void)moveImageStartAtIndex:(int)index withHowMany:(int)howMany{
    [UIView animateWithDuration:2.0 animations:^{
         //Do animation
     }completion:^(BOOL finished) {
         if (finished) {
             if(howMany > 1) {
                  [self moveImageStartAtIndex:index + 1 withHowMany:howMany - 1];
             }
         }
   }];
}