1

我正在通过这段代码为帧设置动画,这在代码中很容易理解,现在我试图在 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);
     }];

}
4

2 回答 2

1

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

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

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

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

于 2013-07-31T09:00:23.833 回答
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);
  }];
 }
于 2013-07-31T08:57:34.347 回答