2013-10-17 15 views
0

我是iPhone新手,我正在使用folling代碼來動畫我的UIButton,它的動畫效果非常完美,但是當我點擊按鈕的動作沒有觸發時。請幫我解決這個問題。如何在兩點之間連續彈跳uibutton

[UIButton animateWithDuration:3.0 
          delay:0.0f 
         options: UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionBeginFromCurrentState 
        animations: ^(void){ 
         CGPoint p = Mybutton.center; 
         p.y += 100; 
         Mybutton.center = p; 

        } 
        completion:NULL]; 
+0

這不顯示我們如何創建按鈕... – RyanR

回答

2

這段代碼是在button action還是viewDidLoad中?試試這個:

- (IBAction)buttonAnimation:(id)sender { 
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; 
animation.duration = .3; 
animation.repeatCount = 5; 
animation.fromValue = [NSValue valueWithCGPoint:yourButton.center]; 
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(yourButton.center.x, yourButton.center.y-30)]; 
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]; 
animation.autoreverses = YES; 
animation.removedOnCompletion = NO; 
[yourButton.layer addAnimation:animation forKey:@"position"];} 

你應該把它放在按鈕動作中。 希望它有幫助

相關問題