0

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

1 回答 1

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"];}

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

于 2013-10-17T14:10:29.867 回答