如何阻止这个序列?

时间:2013-07-31 08:31:08

标签: ios function animation callback

我通过这段代码动画一个框架,这在代码中很容易理解,现在我试图在完成括号后20次意味着停止此序列我只需要调用该函数20次:我该怎么办?是什么?

    -(void)conveyComplete:(UIView*)v
    {
     [self convey:v delay:0];
    }
    -(void)convey:(UIView*)v delay:(int)nDelay
    {
    [UIView animateWithDuration:.5
                          delay:nDelay
                        options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction)
                     animations: ^
     {
         CGRect rPos = v.frame;

         NSLog(@"x:%f y:%f vFrame:%f vFrame:%f" , rPos.origin.x,rPos.origin.y,v.frame.origin.x,v.frame.origin.y);    
             rPos.origin.x -= 5;
             rPos.origin.y -=100;

             v.frame = rPos;

     }
                     completion: ^(BOOL finished)
     {
         [self conveyComplete:v];
          NSLog(@"I:%i, F:%i",i,f);
     }];

}

2 个答案:

答案 0 :(得分:1)

由于您的函数仅执行动画,因此一种可能的解决方案是不要将函数调用20次,而是使用setAnimationRepeatCount:方法设置动画的重复计数。 它看起来与此类似:

[UIView UIView animateWithDuration:.5
                  delay:nDelay
                options:(UIViewAnimationOptionRepeat | ...)
             animations: ^{
                 // Do your animation stuff

                 [UIView setAnimationRepeatCount:20];
             }
             completion:NULL];

但值得关注的另一个方面是你是否需要重复动画20次。您所做的只是逐步移动您的视图框架。为什么不通过设置适当的偏移和动画持续时间来同时为此设置动画?

答案 1 :(得分:0)

我做了一个全局整数:

int i=1;


 -(void)conveyComplete:(UIView*)v{

  [self convey:v delay:0];
  }

 -(void)convey:(UIView*)v delay:(int)nDelay{

 [UIView animateWithDuration:.5
                  delay:nDelay
                options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction)
             animations: ^
   {   

   CGRect rPos = v.frame;
   i+=1;// then increase +1 every time it runs
 NSLog(@"x:%f y:%f vFrame:%f vFrame:%f" , rPos.origin.x,rPos.origin.y,v.frame.origin.x,v.frame.origin.y);


     rPos.origin.x -= 5;

     rPos.origin.y -=100;

     v.frame = rPos;

 }

completion: ^(BOOL finished)
 {
 if(i<20){
 [self conveyComplete:v];
 }
  NSLog(@"I:%i, F:%i",i,f);
  }];
 }