对uibuttons的跳跃效果

时间:2012-10-19 06:08:12

标签: iphone ios uibutton uianimation

在我的应用程序中,我使用移动子视图。如果它达到y = 150,我想在跳跃效果中制作一个按钮,我尝试了这个链接adding bounce effect to appearance of UIImageView它在水平方向工作,我想要一个垂直方向,这是我的代码,

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

请帮我改变跳跃效果(垂直)?

5 个答案:

答案 0 :(得分:18)

试试这个。您可以进行一些更改,以满足您的需求,

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

希望这有帮助。

答案 1 :(得分:4)

试试这段代码..

- (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    
}

查看this链接

的完整答案

答案 2 :(得分:2)

Swift 3

在这里,我发现了一个有趣的流行动画上的有用帖子。希望这是你想要的

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)
}

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

答案 3 :(得分:0)

快速版

    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)
    }

答案 4 :(得分: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
                                }
    })