2013-08-27 86 views
3

我有四個UIbuttons,我必須改變自己的x原點。現在我用這個方法:如何創建一個動畫塊IOS

[UIView animateWithDuration:1.0 
          delay:1.0 
         options: UIViewAnimationCurveEaseInOut 
        animations:^{ 

        self.Button1.frame = CGRectMake(-120, self.Button1.frame.origin.y, self.Button1.frame.size.width, self.Button1.frame.size.height); 

        } 
        completion:^(BOOL finished){ 

         [UIView animateWithDuration:1.0 
          delay:1.0 
         options: UIViewAnimationCurveEaseInOut 
        animations:^{ 

        self.Button1.frame = CGRectMake(-140, self.Button1.frame.origin.y, self.Button1.frame.size.width, self.Button2.frame.size.height); 

        } 
        completion:^(BOOL finished){ 


        }]; 
        }]; 

我把一個動畫在接下來的completition部分。但我有4/5按鈕。我不能這麼做很多次。有沒有辦法做一個物體上的動畫,經過0.5秒的延遲做它的下一個?

編輯:

我試圖用這樣的:

typedef void (^AnimationBlock)(); 

AnimationBlock firstAnimation = ^{ self.Button1.frame = CGRectMake(-120, self.Button1.frame.origin.y, self.Button1.frame.size.width, self.Button1.frame.size.height); }; 

AnimationBlock secondAnimation = ^{ self.Button4.frame = CGRectMake(-120, self.Button4.frame.origin.y, self.Button4.frame.size.width, self.Button4.frame.size.height);}; 

[UIView animateWithDuration:2 animations:firstAnimation completion:^(BOOL finished) { 
    [UIView animateWithDuration:2 animations:secondAnimation]; 
}]; 

,但有沒有可能設置兩個動畫之間的時間。

回答

1

做這樣的事情(在這個例子中,我移動相同的UIButton中的所有動畫因爲我第一次嘗試上的XCode,但你喜歡,你可以改變動畫塊的內容)

typedef void (^AnimationBlock)(); 

// declare two example animations 
AnimationBlock firstAnimation = ^{ 
    CGRect firstFrame = self.aButton.frame; 
    firstFrame.origin.x = firstFrame.origin.x - 20; 
    self.aButton.frame = firstFrame; 
}; 

AnimationBlock secondAnimation = ^{ 
    CGRect firstFrame = self.aButton.frame; 
    firstFrame.origin.x = firstFrame.origin.x - 20; 
    self.aButton.frame = firstFrame; 
}; 

// starting point, create an array of animations 
NSArray *animations = @[firstAnimation, secondAnimation]; 

// first animation starts immediately 
double delayInSeconds = 0.0; 
for (AnimationBlock animation in animations) { 

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 

     [UIView animateWithDuration:0.5f animations:[animation copy]]; 
    }); 

    // first animation starts immediately, then increase the dispatch_after timer of 0.5 sec at each loop 
    delayInSeconds = delayInSeconds + 0.5f; 
} 

本質:

  • 聲明你的動畫塊,有多少,只要你想
  • 把它放在一個數組
  • for循環中:在每個循環中,添加一個動畫隊列後調度。在每個循環中,增加延遲。因此,在循環結束時,您將有X動畫(多達動畫陣列的數量)都開始啓動,在0.5秒的延遲與前一個
2

我會做的只是把你的代碼,使之更加通用。那麼你可以簡單地動畫化任何按鈕。

- (void)animateButton:(UIButton *)button toRect:(CGRect)rect1 thenToRect:(CGRect)rect2 withDelay:(CGFloat)delay 
{ 
    [UIView animateWithDuration:1.0 
          delay:delay 
         options:UIViewAnimationCurveEaseInOut 
        animations:^{ 
         button.frame = rect1; 
        } 
        completion:^(BOOL finished){ 
         [UIView animateWithDuration:1.0 
               delay:delay 
              options:UIViewAnimationCurveEaseInOut 
             animations:^{ 
              button.frame = rect2; 
             } 
             completion:nil]; 
        }]; 
} 

然後你使用這樣的(我簡化了創建矩形代碼,使其更清潔)

[self animateButton:self.button1 
      toRect:CGRectOffset(self.button1.frame, -120 - self.button1.frame.origin.x) 
     thenToRect:CGRectOffset(self.button1.frame, -140 - self.button1.frame.origin.x) 
       delay:0.5f];