Even though these animations can be done by a fade-out animation curve, I also want to use the same damping and stiffness worth of spring animations, so I can slow down the speed. There is no elasticity, just a very smooth transition.
CGFloat dampingstiffness =16.0F;Scale animation for the main app background jnwspringanimation *scale = [Jnwspringanimation animationwithkeypath:@"Transform.scale"];Scale. Damping= Dampingstiffness;Scale. Stiffness= Dampingstiffness;Scale. Mass=1;Scale. Fromvalue= @ ([[Self. Appbackground. Layer. PresentationlayerValueforkeypath:scale. KeyPath] Floatvalue]);Scale. Tovalue= @(0.9);[Self. Appbackground. LayerAddanimation:scale Forkey:scale. KeyPath];Self. Appbackground. Transform= Cgaffinetransformscale (cgaffinetransformidentity,. 9,. 9);
I set the damping and stiffness values to a CGFloat variable so that I can tweak them more easily without having to update two values at a time.
One of the major changes in this block code, unlike other examples, is that the fromvalue of a proportional animation is not set to a constant, but rather to [[Self.appBackground.layer.presentationLayer ValueForKeyPath:scale.keyPath] Floatvalue]. What does that mean? If you take a piece apart, these things are going to happen:
- I will use Self.appbackground to access the Appbackground property of this class
- I'll get to this view of the
CALayer object
- I get the Presentationlayer property on the layer to get a special presentation model layer, so we can see the value when the animation changes
- When I have Presentationlayer, I call-valueforkeypath: To get the current value of the proportional portion of the transformation. (Scale.keypath = @ "Transform.scale")
- When I finally had the current scale value, it was not the data format jnwspringanimation needed, so I used floatvalue.
Remember before I mentioned that many of the property values on the layer in the animation will not change? and the presentation model layer is the core animation used to store the exact change values during the animation? We need to get the current value of the proportional transform so that we can start the animation at any point at the moment (remember that if the user is happy to click, we don't want the animation to start again!). )。 We need to get a special display layer to see the values. Then we use it as the fromvalue of our animations, so that he can always work, whether Fromvalue is the normal, non-touching scale value we set for the view, or a value in the animation. If we don't get it through Presentationlayer, this value will never be correct in the animation until the end of the animation.
Not only do we need to animate the main app background, we also need to animate the map, drop the scale back to 1.0, and move it through the transition to the screen. Let's do it now.
The map has two separate animations, one for the position and one for the proportional jnwspringanimation *mapscale = [Jnwspringanimation animationwithkeypath:@"Transform.scale"];Mapscale. Damping= Dampingstiffness;Mapscale. Stiffness= Dampingstiffness;Mapscale. Mass=1;Mapscale. Fromvalue= @ ([[Self. Mapview. Layer. PresentationlayerValueforkeypath:mapscale. KeyPath] Floatvalue]);Mapscale. Tovalue= @(1.0);[Self. Mapview. LayerAddanimation:mapscale Forkey:mapscale. KeyPath];Self. Mapview. Transform= Cgaffinetransformscale (cgaffinetransformidentity,1,1);Jnwspringanimation *maptranslate = [Jnwspringanimation animationwithkeypath:@"TRANSFORM.TRANSLATION.Y"];Maptranslate. Damping= Dampingstiffness;Maptranslate. Stiffness= Dampingstiffness;Maptranslate. Mass=1;Maptranslate. Fromvalue= @ ([[Self. Mapview. Layer. PresentationlayerValueforkeypath:maptranslate. KeyPath] Floatvalue]);Maptranslate. Tovalue= @(0);[Self. Mapview. LayerAddanimation:maptranslate forkey:maptranslate. KeyPath];Self. Mapview. Transform= Cgaffinetransformtranslate (self. Mapview. Transform,0,0);
There is nothing very complicated here, except to get the current change value to start with, as in the previous animation. I also use the damping and stiffness variables here, so that all animations feel the same type of action.
Locking this up is an orthodox code, fortunately it's very simple, and now you should get used to the look of the Jnwspringanimation code block. This is what the current animation looks like.
Now it's time to add other animations to this interface, that is, when the user clicks on the map icon and the map is visible, we want to fade it out and put the main app background back in front. Because it is exactly like the animation we just showed, in addition to the start and end values, here we put a chunk directly to explain what happened.
if (self. mapshowing) {Self. mapshowing= NO;Use these animations again with the same damping and stiffness//so that we can get the values in cgfloat form. Note that this value is a bit higher//means that the animation will take less time (under the spring animation that matches this damping and stiffness). Less time is good, because we want to go back to the default state of the interface, when the user just wants to let the map disappear quickly. CGFloat dampingstiffnessout =24.0F;Again, it is important to start from the current state so that when the user taps the button it does not twitch [UIView animatewithduration:. 5Delay0Options:uiviewanimationoptioncurveeaseinout| Uiviewanimationoptionbeginfromcurrentstate animations:^{Self. Appbackground. Alpha=1.0F;} Completion:null];[UIView animatewithduration:. 3Delay0Options:uiviewanimationoptioncurveeaseinout| Uiviewanimationoptionbeginfromcurrentstate animations:^{Self. Mapview. Alpha=0.0F;} Completion:null];The map has two separate animations, one for the position and the other for the proportions. We get @ "transform by Presentationlayer. scale"The value of the change, as in the previous example jnwspringanimation *mapscale = [jnwspringanimationanimationwithkeypath:@"Transform.scale"];Mapscale. Damping= Dampingstiffnessout;Mapscale. Stiffness= Dampingstiffnessout;Mapscale. Mass=1;Mapscale. Fromvalue= @ ([[Self. Mapview. Layer. PresentationlayerValueforkeypath:mapscale. KeyPath] Floatvalue]);Mapscale. Tovalue= @(1.1);[Self. Mapview. LayerAddanimation:mapscale Forkey:mapscale. KeyPath];Self. Mapview. Transform= Cgaffinetransformscale (cgaffinetransformidentity,1.1,1.1);Jnwspringanimation *maptranslate = [Jnwspringanimation animationwithkeypath:@"TRANSFORM.TRANSLATION.Y"];Maptranslate. Damping= Dampingstiffnessout;Maptranslate. Stiffness= Dampingstiffnessout;Maptranslate. Mass=1;Maptranslate. Fromvalue= @ ([[Self. Mapview. Layer. PresentationlayerValueforkeypath:maptranslate. KeyPath] Floatvalue]);Maptranslate. Tovalue= @( -);[Self. Mapview. LayerAddanimation:maptranslate forkey:maptranslate. KeyPath];Self. Mapview. Transform= Cgaffinetransformtranslate (self. Mapview. Transform,0, -);The scale animation of the main app background. We animate it back to1.0Times Jnwspringanimation *scale = [Jnwspringanimation animationwithkeypath:@"Transform.scale"];Scale. Damping= Dampingstiffnessout;Scale. Stiffness= Dampingstiffnessout;Scale. Mass=1;Scale. Fromvalue= @ ([[Self. Appbackground. Layer. Presentationlayervalueforkeypath:@"Transform.scale.x"] Floatvalue]);Scale. Tovalue= @(1.0);[Self. Appbackground. LayerAddanimation:scale Forkey:scale. KeyPath];Self. Appbackground. Transform= Cgaffinetransformscale (cgaffinetransformidentity,1.0,1.0);}
Here is the full, final animation look. If you want to click on a madman, you will find that it is actually animated from the current value and does not twitch.
This is interesting! Now let's go to the blink of an eye with some choppy animations.
Finished viewing the integration set: Https://github.com/Cloudox/Motion-Design-for-iOS
All rights reserved: Http://blog.csdn.net/cloudox_
Motion Design for IOS (33)