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!
You don't need to use [weak self] in static function UIView. animate() You need to use weak when retain cycle is possible and animations block is not retained by self.
The contents of your block are performed on the main thread regardless of where you call [UIView animateWithDuration:animations:] . It's best to let the OS run your animations; the animation thread does not block the main thread -- only the animation block itself.
UIView. animate runs on the main thread and is asynchronous.
A simpler alternative to UIDynamicAnimator
in iOS 7 is Spring Animation (a new and powerful UIView block animation), which can give you nice bouncing effect with damping and velocity: Objective C
[UIView animateWithDuration:duration delay:delay usingSpringWithDamping:damping initialSpringVelocity:velocity options:options animations:^{ //Animations } completion:^(BOOL finished) { //Completion Block }];
Swift
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 })
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 })
usingSpringWithDamping
0.0 == very bouncy. 1.0 makes it smoothly decelerate without overshooting.
initialSpringVelocity
is, roughly, "desired distance, divided by desired seconds". 1.0 corresponds to the total animation distance traversed in one second. Example, total animation distance is 200 points and you want the start of the animation to match a view velocity of 100 pt/s, use a value of 0.5.
More detailed tutorial and sample app can be found in this tutorial. I hope this is useful for someone.
With iOS7 and UIKit Dynamics, there is no longer any need to use CAKeyframeAnimations
or UIView
animations!
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.
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];
And here are the results
UIKit Dynamics is a really powerful and easy to use addition to iOS7 and you can get some great looking UI from it.
Other examples:
The steps to implement UIKit dynamics is always the same:
UIDynamicAnimator
and store it in a strong propertyUIDynamicBehaviors
. Each behavior should have one or more items, typically a view to animate.UIDynamicBehaviors
is a valid state within the UIDynamicAnimator
simulation.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With