如何在两点之间连续弹跳uibutton

时间:2013-10-17 13:52:15

标签: ios uibutton core-animation quartz-core

我是iPhone新手,我正在使用folling代码为我的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];

1 个答案:

答案 0 :(得分:2)

这个代码是按钮动作还是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"];}

你应该把它放在按钮动作中。 希望它有所帮助