Search code examples
iosuibuttoncore-animationquartz-core

how to continuous bouncing uibutton between two points


I'm new to iPhone developing,am using folling code to animate my UIButton it's animating perfectly but when i click the button the action not firing. plese help me out from this.

 [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];

Solution

  • Is this code in the button action or viewDidLoad? Try this one :

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

    You should put it in the button action. Hope it helps