如何创建 UIView 弹跳动画? [英] How to create a UIView bounce animation?

查看:38
本文介绍了如何创建 UIView 弹跳动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following CATransition for a UIView called finalScoreView, which makes it enter the screen from the top:

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

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

How do I make it so it bounces once after it comes down, then stays still? It should still enter the screen from the top but then bounce when it comes down.

非常感谢任何帮助,谢谢!

Any help would be much appreciated, thanks!

推荐答案

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

With iOS7 and UIKit Dynamics, there is no longer any need to use CAKeyframeAnimations or UIView animations!

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

Take a look at Apple's UIKit Dynamics Catalog app. Alternately, Teehanlax has a clear, concise tutorial with the full project in github. If you want a more detailed tutorial about the ins-and-outs of dynamics, the Ray Winderlich tutorial is great. As always, the Apple docs are a great first stop, so check out the UIDynamicAnimator Class reference in the docs.

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

Here's a bit of the code from the Teenhanlax tutorial:

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.

UIKit Dynamics is a really powerful and easy to use addition to iOS7 and you can get some great looking UI from it.

其他示例:

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

The steps to implement UIKit dynamics is always the same:

  1. 创建一个 UIDynamicAnimator 并将其存储在一个强大的属性中
  2. 创建一个或多个UIDynamicBehaviors.每个行为都应该有一个或多个项目,通常是一个动画视图.
  3. 确保 UIDynamicBehaviors 中使用的项目的初始状态是 UIDynamicAnimator 模拟中的有效状态.
  1. Create a UIDynamicAnimator and store it in a strong property
  2. Create one or more UIDynamicBehaviors. Each behavior should have one or more items, typically a view to animate.
  3. Make sure that the initial state of the items used in the UIDynamicBehaviors is a valid state within the UIDynamicAnimator simulation.

这篇关于如何创建 UIView 弹跳动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆