如何创建 UIView 弹跳动画? [英] How to create a UIView bounce animation?
问题描述
我有一个名为 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,不再需要使用 CAKeyframeAnimations
或 UIView
动画!
With iOS7 and UIKit Dynamics, there is no longer any need to use CAKeyframeAnimations
or UIView
animations!
看看 Apple 的 UIKit Dynamics Catalog 应用一>.或者,Teehanlax 有一个清晰、简洁的教程,其中
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:
- 创建一个
UIDynamicAnimator
并将其存储在一个强大的属性中 - 创建一个或多个
UIDynamicBehaviors
.每个行为都应该有一个或多个项目,通常是一个动画视图. - 确保
UIDynamicBehaviors
中使用的项目的初始状态是UIDynamicAnimator
模拟中的有效状态.
- Create a
UIDynamicAnimator
and store it in a strong property - Create one or more
UIDynamicBehaviors
. Each behavior should have one or more items, typically a view to animate. - Make sure that the initial state of the items used in the
UIDynamicBehaviors
is a valid state within theUIDynamicAnimator
simulation.
这篇关于如何创建 UIView 弹跳动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!