4

在我的应用程序中,我使用了移动子视图。如果它达到 y=150,我想制作一个跳跃效果的按钮,我尝试了这个链接,将反弹效果添加到 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];  
}
}

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

4

5 回答 5

19

尝试这个。您可以进行一些更改,使其适合您的需要,

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

希望这可以帮助。

于 2012-10-19T10:30:31.790 回答
6

斯威夫特 3

在这里,我找到了一篇关于这个有吸引力的流行动画的有用帖子。希望这是你想要的

在此处输入图像描述

@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/

于 2017-10-02T15:19:03.770 回答
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    
}

从此链接查看完整答案

于 2012-10-19T06:20:08.943 回答
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
                                }
    })
于 2016-07-22T10:57:25.187 回答
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)
    }
于 2015-08-22T09:17:49.057 回答