2011-12-21 46 views
1

我想用UIAnimation的塊風格的方法,使一個按鈕閃爍,尤其是:我可以在UIView動畫塊中使用setAnimationRepeatCount:

animateWithDuration:delay:options:animations:completion 

有一個問題我想我需要設置的東西是「AnimationRepeatCount」屬性 - 可以這樣的動畫塊代碼中設置喜歡這個?

- (void)animateItemWithBlinking:(BOOL)blinking { 

    __block BOOL isInBlinkState = blinking; 

    if (!isBlinking) 
    {  
     // Start blinking     
     [UIView animateWithDuration:0.50f 
      delay:0 
      options:UIViewAnimationCurveLinear 
      animations:^{ 
       // Can I call the AnimationRepeat setter here or 
       // should it be elsewhere outside this block? 
       [UIView setAnimationRepeatCount:1000]; 
       [UIView setAnimationRepeatAutoreverses:YES]; 

       [button setAlpha:0.0f];   
      } completion:^(BOOL finished) { 
       // eventually, this is a value that I want the method to return 
       isInBlinkState = !isInBlinkState;   
     }];   
    } 
    else 
    { 
     // Stop blinking 
     [UIView animateWithDuration:0.30f 
      delay:0 
      options:UIViewAnimationOptionAllowUserInteraction 
      animations:^{ 
       // Stop blinking - reset everything 
       [UIView setAnimationBeginsFromCurrentState:YES]; 
       [UIView setAnimationRepeatCount:1]; 

       [button setAlpha:1.0f];   
     } completion:^(BOOL finished) { 
      // eventually, this is a value that I want the method to return 
      isInBlinkState = !isInBlinkState;   
     }]; 
    } 
} 

此前基於塊的電話,我原來的方法是這樣的:

- (void)animateItemWithBlinking:(BOOL) blinking { 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.50f]; 
    [UIView setAnimationCurve:UIViewAnimationCurveLinear]; 

    if(!blinking) {  
     // Start it 
     [UIView setAnimationRepeatCount:1000]; 
     [UIView setAnimationRepeatAutoreverses:YES]; 

     [button setAlpha:0.0f];     
    } else { 
     // Stop it 
     [UIView setAnimationBeginsFromCurrentState:YES]; 
     [UIView setAnimationRepeatCount:1]; 

     [button setAlpha:1.0f];   
    } 
    blinking = !blinking; 
    [UIView commitAnimations]; 
} 
+0

您可以看看與您的問題類似的問題。 http://stackoverflow.com/questions/4069758/repeat-count-for-uiview-block-based-animation – 2011-12-21 23:13:52

回答

1

是的,你可以將它設置塊內,它會做你所期望。