2012-07-12 29 views
1

我試圖在它通過改變α水平來回一次的觀點負載跳動緩慢,以至於有一個圖像的UIButton ...使uibutton脈動。

目前我在做這一點,但什麼都不做....

-(void)loopdyloop 
{ 
    while (myCount < 1000) 
    { 

     [UIView animateWithDuration:3.0 
           delay:0.0f 
          options:UIViewAnimationCurveEaseOut 
         animations:^{ 


          iconButton.alpha = 0.0; 


         } completion:^(BOOL finished) { 
          if (finished) { 

           NSLog(@"animated"); 
          } 
         }]; 


     [UIView animateWithDuration:3.0 
           delay:0.0f 
          options:UIViewAnimationCurveEaseOut 
         animations:^{ 


          iconButton.alpha = 1.0; 


         } completion:^(BOOL finished) { 
          if (finished) { 

           NSLog(@"animated"); 
          } 
         }]; 




     myCount++; 
    } 

} 

這種方法是從viewDidLoad方法調用,

相當粗糙,但我知道它我最好的嘗試,如果你對我怎麼能做到這一點,將不勝感激的任何想法。

回答

14

如何在viewDidLoad在您的按鈕層應用此:

CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
pulseAnimation.duration = .5; 
pulseAnimation.toValue = [NSNumber numberWithFloat:1.1]; 
pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
pulseAnimation.autoreverses = YES; 
pulseAnimation.repeatCount = FLT_MAX; 
[YOURButton.layer addAnimation:pulseAnimation forKey:nil]; 

和你可以試用它。

+0

我實際上看到了類似這個代碼的東西..但我無法弄清楚如何將它應用到一個uibutton。我認爲會有更多的東西不僅僅是mybutton.layer:P:P感謝你的高舉!動畫真棒! :P – HurkNburkS 2012-07-12 20:30:05

+0

哈哈哈,我也是。我想你只需要在你的按鈕層應用它。所以goodluck :) – Bazinga 2012-07-13 00:29:27

+0

這看起來不錯! – pob21 2012-09-24 18:56:59

5

如果你想讓它重複永遠可以添加動畫選項,自動反轉和重複,像這樣:

[UIView animateWithDuration:3.0 
         delay:0.0f 
        options:UIViewAnimationCurveEaseOut | 
          UIViewAnimationOptionRepeat | 
          UIViewAnimationOptionAutoreverse 
       animations:^{ 
        iconButton.alpha = 0.0; 
       } 
       completion:^(BOOL finished) { 
        // You could do something here 
       }]; 

這將導致阿爾法第一動畫到0.0,然後再返回到原來的( 1.0),然後做這兩件事情一遍又一遍又一遍......