如何创build一个UIView反弹animation?

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

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

我该如何做到这一点,所以它一旦弹下来,然后停下来呢? 它应该仍然从顶部进入屏幕,但是当它下降时会弹起。

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

使用iOS7和UIKit Dynamics,不再需要使用CAKeyframeAnimationsUIViewanimation!

看看苹果的UIKitdynamic目录应用程序 。 或者, Teehanlax 在github上提供了一个清晰,简明的完整项目 教程 。 如果你想要更详细的dynamic教程 , Ray Winderlich教程非常棒。 与往常一样,苹果文档是一个很好的第一站,所以请查看文档中的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]; 

这里是结果

广场反弹

UIKit Dynamics是一个非常强大和易于使用的除了iOS7,你可以从它得到一些漂亮的UI。

其他例子:

按钮反弹幻灯片反弹春天收集WWDC短跑集合

实现UIKitdynamic的步骤总是相同的:

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

在iOS 7中, UIDynamicAnimator一个简单的替代方法是Spring Animation(一个新的强大的UIView块animation),它可以给你很好的阻尼和速度弹跳效果: Objective C

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

迅速

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

usingSpringWithDamping 0.0 ==非常有弹性。 1.0使其平稳减速而不会超调。

initialSpringVelocity大致是“所需的距离,除以所需的秒数”。 1.0对应于一秒内遍历的总animation距离。 例如,总animation距离为200点,您希望animation的开头匹配100pt / s的视图速度,使用值为0.5。

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

这里是我创build的演示项目,以帮助您获得正确的animation。 请享用!

SpringDampingDemo

在这里输入图像描述

 - (IBAction)searchViewAction:(UIButton*)sender { if(sender.tag == 0) { sender.tag = 1; CGRect optionsFrame2 = self.defaultTopView.frame; optionsFrame2.origin.x = -320; CGRect optionsFrame = self.searhTopView.frame; optionsFrame.origin.x = 320; self.searhTopView.frame = optionsFrame; [UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity:1.0 options:0 animations:^{ CGRect optionsFrame = self.searhTopView.frame; optionsFrame.origin.x = 0; self.searhTopView.frame = optionsFrame; self.defaultTopView.frame = optionsFrame2; } completion:^(BOOL finished) { }]; } else { sender.tag = 0; CGRect optionsFrame2 = self.defaultTopView.frame; optionsFrame2.origin.x = 0; CGRect optionsFrame = self.searhTopView.frame; optionsFrame.origin.x = 320; [UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity:1.0 options:0 animations:^{ CGRect optionsFrame = self.searhTopView.frame; optionsFrame.origin.x = 320; self.searhTopView.frame = optionsFrame; self.defaultTopView.frame = optionsFrame2; } completion:^(BOOL finished) { }]; } }