4

Here in my app i used the moving sub View.If it reach y=150,i want to make a buttons in jumping effect,i tried this link adding bounce effect to appearance of UIImageView it working in horizontal direction ,i want a vertical direction,Here my code,

-(void)godown 
 {
if (movingview.center.y < 150) movingview.center = CGPointMake(movingview.center.x, movingview.center.y +5);
if(movingview.center.y ==150)
{
  [UIView beginAnimations:@"bounce" context:nil];
  [UIView setAnimationRepeatCount:3];
  [UIView setAnimationRepeatAutoreverses:YES];  
  CGAffineTransform transform = CGAffineTransformMakeScale(1.3,1.3);
  bt1.transform = transform;
  bt2.transform=transform;
 [UIView commitAnimations];  
}
}

Please help me to change into jumping effect(vertically)?

Community
  • 1
  • 1
iosdev
  • 277
  • 8
  • 19

5 Answers5

19

Try this. You can make some changes so it suits your need,

        CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform"];
        anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        anim.duration = 0.125;
        anim.repeatCount = 1;
        anim.autoreverses = YES;
        anim.removedOnCompletion = YES;
        anim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)];
        [senderView.layer addAnimation:anim forKey:nil];

Hope this helps.

Mathew Varghese
  • 4,527
  • 2
  • 17
  • 26
6

Swift 3

Here i found a useful post on this attractive pop animation. Hope it is what you want

enter image description here

@IBAction func btnCancel_click(_ sender: Any)
{
    btnCancel.transform = CGAffineTransform(scaleX: 0.50, y: 0.50)
    UIView.animate(withDuration: 2.0,
                   delay: 0,
                   usingSpringWithDamping: 0.2,
                   initialSpringVelocity: 6.0,
                   options: .allowUserInteraction,
                   animations: { [weak self] in
                    self?.btnCancel.transform = .identity
        },
                   completion: nil)
}

source: http://evgenii.com/blog/spring-button-animation-with-swift/

Vaibhav Saran
  • 12,848
  • 3
  • 65
  • 75
4

Try this code..

- (IBAction)bounce {
    CABasicAnimation *theAnimation;
    theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"];///use transform
    theAnimation.duration=0.4;  
    theAnimation.repeatCount=2;
    theAnimation.autoreverses=YES;  
    theAnimation.fromValue=[NSNumber numberWithFloat:1.0]; 
    theAnimation.toValue=[NSNumber numberWithFloat:-20];
    [yourButton.layer addAnimation:theAnimation forKey:@"animateTranslation"];//animationkey    
}

see the whole answer from this link

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
0

And the swift version

    func jumpButtonAnimation(sender: UIButton) {
        let animation = CABasicAnimation(keyPath: "transform.scale")
        animation.toValue = NSNumber(float: 1.3)
        animation.duration = 0.1
        animation.repeatCount = 0
        animation.autoreverses = true
        sender.layer.addAnimation(animation, forKey: nil)
    }
jose920405
  • 7,982
  • 6
  • 45
  • 71
Allreadyhome
  • 1,252
  • 2
  • 25
  • 46
0
UIView.animateWithDuration(0.1 ,
                               animations: {
                                self.buttonName.transform = CGAffineTransformMakeScale(1.3, 1.3)
        },
                                completion: { finish in
                                UIView.animateWithDuration(0.1){
                                self.buttonName.transform = CGAffineTransformIdentity
                                }
    })
Ankit garg
  • 51
  • 1
  • 4
  • Can you add some explanation to your answer? Posting just a code snippet is not a good way. See [How to ask](http://stackoverflow.com/help/how-to-ask) – Vladimir Vagaytsev Jul 22 '16 at 11:17