如何创建UIView反弹动画?

use*_*576 88 iphone animation uikit uiview ios

我有一个名为UIView的以下CATransition finalScoreView,它使它从顶部进入屏幕:

CATransition *animation = [CATransition animation];
animation.duration = 0.2;
animation.type = kCATransitionPush;
animation.subtype = kCATransitionFromBottom;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

[gameOver.layer addAnimation:animation forKey:@"changeTextTransition"];
[finalScoreView.layer addAnimation:animation forKey:@"changeTextTransition"];
Run Code Online (Sandbox Code Playgroud)

如何使它在它下降后反弹一次然后保持静止?它应该仍然从顶部进入屏幕,但在它关闭时反弹.

任何帮助将不胜感激,谢谢!

huo*_*ong 254

UIDynamicAnimator在iOS 7中更简单的替代方案是Spring Animation(一种新的强大的UIView块动画),它可以通过阻尼和速度为您提供良好的弹跳效果: Objective C

[UIView animateWithDuration:duration
  delay:delay
  usingSpringWithDamping:damping
  initialSpringVelocity:velocity
  options:options animations:^{
    //Animations
    }
  completion:^(BOOL finished) {
    //Completion Block
}];
Run Code Online (Sandbox Code Playgroud)

迅速

UIView.animateWithDuration(duration,
     delay: delay,
     usingSpringWithDamping: damping,
     initialSpringVelocity: velocity,
     options: options,
     animations: {
            //Do all animations here
            }, completion: {
            //Code to run after animating
                (value: Bool) in
        })
Run Code Online (Sandbox Code Playgroud)

Swift 4.0

UIView.animate(withDuration:duration,
     delay: delay,
     usingSpringWithDamping: damping,
     initialSpringVelocity: velocity,
     options: options,
     animations: {
            //Do all animations here
            }, completion: {
            //Code to run after animating
                (value: Bool) in
        })
Run Code Online (Sandbox Code Playgroud)

usingSpringWithDamping 0.0 ==非常有弹性.1.0使其平稳减速而不会过冲.

initialSpringVelocity粗略地说,是"所需距离除以所需的秒数".1.0对应于一秒内遍历的总动画距离.例如,总动画距离为200点,您希望动画的开始与100 pt/s的视图速度匹配,使用值0.5.

在本教程中可以找到更详细的教程和示例应用程序.我希望这对某人有用.

  • 这是我创建的一个演示项目,可以帮助您获得恰到好处的动画效果.请享用!https://github.com/jstnheo/SpringDampingDemo (2认同)

mem*_*ons 142

使用iOS7和UIKit Dynamics,不再需要使用CAKeyframeAnimationsUIView动画!

看看Apple的UIKit Dynamics Catalog应用程序.或者,Teehanlax有一个清晰,简洁的教程,在github中完整的项目.如果你想要一个关于动力学细节的更详细的教程,Ray Winderlich教程非常棒.与往常一样,Apple文档是一个很好的第一站,因此请查看文档中的UIDynamicAnimator类参考.

以下是Teenhanlax教程的一些代码:

self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

UIGravityBehavior* gravityBehavior = 
                [[UIGravityBehavior alloc] initWithItems:@[self.redSquare]];
[self.animator addBehavior:gravityBehavior];

UICollisionBehavior* collisionBehavior = 
                [[UICollisionBehavior alloc] initWithItems:@[self.redSquare]]; 
collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
[self.animator addBehavior:collisionBehavior];

UIDynamicItemBehavior *elasticityBehavior = 
                [[UIDynamicItemBehavior alloc] initWithItems:@[self.redSquare]];
elasticityBehavior.elasticity = 0.7f;
[self.animator addBehavior:elasticityBehavior];
Run Code Online (Sandbox Code Playgroud)

以下是结果

方形反弹

UIKit Dynamics是iOS7中非常强大且易于使用的附加功能,您可以从中获得一些外观漂亮的UI.

其他例子:

按钮反弹 滑动弹跳 有弹性的系列 WWDC冲刺系列

实现UIKit动态的步骤始终相同:

  1. 创建一个UIDynamicAnimator并将其存储在一个强大的属性中
  2. 创建一个或多个UIDynamicBehaviors.每个行为都应该有一个或多个项目,通常是动画视图.
  3. 确保UIDynamicBehaviorsUIDynamicAnimator模拟中使用的项目的初始状态是有效状态.

  • 一个MAGNIFICENT教程,但你只需使用一行代码,使用SpringWithDamping进行反弹! (2认同)
  • 你能分享一些 Uitableview 弹跳动画的代码示例吗?我正在尝试使用 UITableview 但不确定从哪里开始。 (2认同)
  • 请添加完整代码。您的代码描述了如何创建`UIDynamicAnimator` 和与之关联的对象,但没有回答如何使用它们 (2认同)

jst*_*stn 32

这是我创建的一个演示项目,可以帮助您获得恰到好处的动画效果.请享用!

SpringDampingDemo

在此输入图像描述