iOS development-graphics programming oc & Sticky animations and jelly effects

Source: Internet
Author: User
<span id="Label3"></p>Sticky animations and jelly effects<p><p>In a recent custom pagecontrol--kyanimatedpagecontrol, I implemented the Calayer animation and the elastic animation of calayer, which was first reviewed:</p></p><p style="text-align: center; text-indent: 0em;"><p style="text-align: center; text-indent: 0em;"></p></p><p><p>Let's make an outline:</p></p> <ol class=" list-paddingleft-2"> <ol class=" list-paddingleft-2"> <li><p>The first shared theme was " <strong>How to make Calayer happen</strong> ", A technique that was involved in a project ———— Kycuteview before me, and also wrote a brief implementation blog Post. Let me cite another example today.</p></li> <li><p>I've also had an elastic animation like a jelly effect before, like this project--kygooeymenu. The core technique used is cakeyframeanimation, and then the key frames of several different states are set up to achieve this kind of elasticity effect initially. however, after all, there are only a few keyframes, and it is necessary to manually calculate, imprecise not to say, animation is not delicate enough, after all, you can not manually create 60 keyframes. so, the second topic today Is-"how to create 60 keyframes with the damped vibration function", so that the Calayer produces a similar [UIView animateWithDuration:delay:usingSpringWithDamping: initialSpringVelocity:options:animations:completion] Elastic Animation.</p></li> </ol> </ol><p><p>Body.</p></p><p><p><strong>How to make Calayer deformation?</strong></p></p><p><p>The key technique is simple: you need to "spell" the layer with multiple Bezier curves. The reason why we do this is self-evident, because it is convenient for us to Deform.</p></p><p><p>such as Kyanimatedpagecontrol in the small ball, in fact, it is so drawn out:</p></p><p style="text-align: center; text-indent: 0em;"><p style="text-align: center; text-indent: 0em;"></p></p><p><p>The small ball is composed of Arc ab, Arc bc, arc cd, arc da four, each arc is bound to two control points: arc AB binding is C1, C2; arc BC bound is C3, C4 ...</p></p><p><p><strong>How to express each point?</strong></p></p><p><p>First of all, a, B, C and D are four dynamic points, and the variable that controls them is the contentoffset.x of SCROLLVIEW. We can get this variable in real time in-(void) scrollviewdidscroll: (uiscrollview *) scrollview, and convert it to a factor that controls the 0~1, named Factor.</p></p><pre class="brush:js;toolbar:false"><pre class="brush:js;toolbar:false">_factor = MIN (1, MAX (0, (ABS (scrollview.contentoffset.x-self.lastcontentoffset)/scrollView.frame.size.width));</pre></pre><p><p>Suppose that the maximum variation of a, B, C, D is 2/5 of the diameter of the Ball. So combined with this 0~1 coefficient, we can draw a, B, C, D of the real change distance extra: extra = (self.width * 2/5) * factor. When factor = = 1 o'clock, The maximum deformation state is reached, at which point the change distance of four points is (self.width * 2/5).</p></p><p><p><strong>note:</strong> Depending on the direction of the slide, we also need to move from point B or D.</p></p><pre><pre> <span style="color: #008080;">1 </span> cgpoint pointa = cgpointmake (rectcenter.x, self.currentrect.origin.y +< Span style= "color: #000000;" > extra); <span style="color: #008080;">2 </span> cgpoint pointb = cgpointmake (self.scrolldirection = = scrolldirectionleft?) Rectcenter.x + self.currentrect.size.width/<span style="color: #800080;">2 </span>: rectcenter.x + Self.currentrect.size.width/<span style="color: #800080;">2 </span> + extra*<span style="color: #800080;">2 </span> <span style="color: #000000;" ", rectcenter.y); < span> <span style="color: #008080;">3 </span> cgpoint pointc = cgpointmake (rectcenter.x, rectcenter.y + Self.currentrect.size.height/<span style="color: #800080;">2 </span>-<span style="color: #000000;"> extra); </span> <span style="color: #008080;">4 </span> cgpoint pointd = cgpointmake (self.scrolldirection = = scrolldirectionleft?) Self.currentrect.origin.x-extra*<span style="color: #800080;">2 </span>: self.currentrect.origin.x, rectcenter.y) ;</span></pre></pre><p><p></p></p><p><p>Then the control point:</p></p><p><p>The key is to know the a-c1, b-c2, b-c3, c-c4 .... These horizontal and vertical dashed lengths are named Offset. After many attempts, I came to the conclusion that:</p></p><p><p>When offset is set to the diameter divided by 3.6, The arc is perfectly attached to the synthetic arc. I vaguely feel that this 3.6 is inevitable, seemingly and 360 degrees have some kind of relationship, perhaps through the calculation can draw 3.6 This value of inevitability, but I did not try.</p></p><p><p>therefore, the coordinates of each control point are:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span>Cgpoint C1 = Cgpointmake (pointa.x +<span style="color: #000000;"><span style="color: #000000;">offset, pointa.y); </span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span>Cgpoint C2 = Cgpointmake (pointb.x, pointb.y-<span style="color: #000000;"><span style="color: #000000;">offset);</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span>Cgpoint C3 = Cgpointmake (pointb.x, pointb.y +<span style="color: #000000;"><span style="color: #000000;">offset); </span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span>Cgpoint C4 = cgpointmake (pointc.x +<span style="color: #000000;"><span style="color: #000000;">offset, pointc.y);</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span>Cgpoint c5 = cgpointmake (pointc.x-<span style="color: #000000;"><span style="color: #000000;">offset, pointc.y); </span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span>Cgpoint c6 = cgpointmake (pointd.x, pointd.y +<span style="color: #000000;"><span style="color: #000000;">offset);</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span>Cgpoint c7 = Cgpointmake (pointd.x, pointd.y-<span style="color: #000000;"><span style="color: #000000;">offset); </span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span>Cgpoint C8 = Cgpointmake (pointa.x-offset, pointa.y);</pre><p><p></p></p><p><p>With the end point and control point, you can use the method provided in Uibezierpath-(void) addcurvetopoint: (cgpoint) endPoint controlPoint1: (cgpoint) controlPoint1 ControlPoint2: (cgpoint) controlPoint2; Draw a Line.</p></p><p><p>Heavy-duty Calayer-(void) drawincontext: (cgcontextref) ctx; method, in which a pattern is drawn:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span>- (<span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">) drawincontext: (cgcontextref) ctx{</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span>....<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">calculate the coordinates of each point here</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span>uibezierpath* Ovalpath =<span style="color: #000000;"><span style="color: #000000;">[uibezierpath bezierpath];</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span> <span style="color: #000000;"><span style="color: #000000;">[ovalpath movetopoint:pointa];</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #000000;"><span style="color: #000000;">[ovalpath addcurvetopoint:pointb controlpoint1:c1 controlpoint2:c2];</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #000000;"><span style="color: #000000;">[ovalpath ADDCURVETOPOINT:POINTC controlpoint1:c3 controlpoint2:c4];</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #000000;"><span style="color: #000000;">[ovalpath addcurvetopoint:pointd controlpoint1:c5 controlpoint2:c6];</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #000000;"><span style="color: #000000;">[ovalpath Addcurvetopoint:pointa controlpoint1:c7 controlpoint2:c8];</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span> <span style="color: #000000;"><span style="color: #000000;">[ovalpath closepath];</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #000000;"><span style="color: #000000;">Cgcontextaddpath (ctx, ovalpath.cgpath);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #000000;"><span style="color: #000000;">Cgcontextsetfillcolorwithcolor (ctx, self.indicatorColor.CGColor);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span> <span style="color: #000000;"><span style="color: #000000;">Cgcontextfillpath (ctx);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span>}</pre><p><p></p></p><p><p>now, when you slide the scrollview, the ball will Deform.</p></p><p><p><strong>How do I create 60 keyframes with the damped vibration function?</strong></p></p><p><p>In the above example, there is a very important factor, that is, ScrollView in the contentoffset.x this variable, without this input, then nothing will Happen. But to get this variable, you need the user to touch, swipe to create the Interaction. In an animation, the user is not directly interactive input, such as when the finger left, to let the ball to the jelly effect bounce back to the original state, the process finger has left the screen, there is no input, then the above method will certainly not work, so we can use Caanimation.</p></p><p><p>We know that iOS7 Apple in UIView (uiviewanimationwithblocks) has added a new factory method for making elastic animations:</p></p><pre><pre>+ (<span style="color: #0000ff;">void</span><br><br>Options: (uiviewanimationoptions) options animations: (<span style="color: #0000ff;">void</span> (^) (<span style="color: #0000ff;">void</span><br>Completion: (<span style="color: #0000ff;">void</span> (^) (BOOL FINISHED)) completion Ns_available_ios (7_0);</pre></pre><p><p></p></p><p><p>But there is no direct caanimation subclass of elasticity, like Cabasicanimation or cakeyframeanimation, to animate Calayer directly. The good news is that the iOS9 has been added to the public Caspringanimation. But for a low-compatibility version and a knowledge-seeking perspective, we can look at how to manually create an elastic animation for calayer.</p></p><p><p>Before you begin, you need to review the physics of high school ———— damped vibration, You can click the Highlight font link to review a little.</p></p><p style="text-align: center; text-indent: 0em;"><p style="text-align: center; text-indent: 0em;"></p></p><p style="text-align: center; text-indent: 0em;"><p style="text-align: center; text-indent: 0em;"></p></p><p><p>According to wikipedia, we can get the following general formula for vibrating functions:</p></p><p style="text-align: center; text-indent: 0em;"><p style="text-align: center; text-indent: 0em;"></p></p><p><p>Of course this is just a general formula, we need to get the image over (0,0), and finally decay to 1. We can make the original image flip 180 degrees around the x-axis, that is, add a minus SIGN. Then pan up one unit along the Y-axis. So a little deformation can get the following function:</p></p><p style="text-align: center; text-indent: 0em;"><p style="text-align: center; text-indent: 0em;"></p></p><p><p>Want to see the image of the function? No problem, recommend an online view of the function image of the site--desmos, the formula 1-\left (e^{-5x}\cdot \cos (30x) \right) to copy the paste into the image can be Seen.</p></p><p><p>The improved function image is this:</p></p><p style="text-align: center; text-indent: 0em;"><p style="text-align: center; text-indent: 0em;"></p></p><p><p>Perfect to meet our graphics over (0,0), vibration attenuation to 1 of the Requirements. Where 5 of the equation is equivalent to the damping coefficient, the smaller the value, the greater the magnitude of the equation, 30 is equivalent to the oscillation frequency, the greater the number of shocks the More.</p></p><p><p>The next step is to convert to Code.</p></p><p><p>The general idea is to create a 60 frame keyframe (because The screen has a maximum refresh rate of 60FPS) and then assign the 60 frame data to the cakeyframeanimation values Property.</p></p><p><p>After generating 60 frames with the following code, save to an array and return it, WHERE//1 is creating 60 values using the formula just Now:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span>+ (nsmutablearray *) animationvalues: (<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span>) Fromvalue Tovalue: (<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><span style="color: #000000;"><span style="color: #000000;">) Tovalue usingspringwithdamping: (cgfloat) damping initialspringvelocity: (cgfloat) Velocity duration: (cgfloat) duration{</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">60 key Frames</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span>Nsinteger numofpoints = Duration *<span style="color: #800080;"><span style="color: #800080;"></span> -</span><span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span>Nsmutablearray *values =<span style="color: #000000;"><span style="color: #000000;">[nsmutablearray arraywithcapacity:numofpoints];</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(nsinteger i =<span style="color: #800080;"><span style="color: #800080;">0</span></span>; I < numofpoints; i++<span style="color: #000000;"><span style="color: #000000;">) {</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span>[values addobject:@ (<span style="color: #800080;"><span style="color: #800080;">0.0</span></span><span style="color: #000000;"><span style="color: #000000;">)];</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span> <span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Difference Value</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span>CGFloat d_value = [tovalue floatvalue]-<span style="color: #000000;"><span style="color: #000000;">[fromvalue floatvalue];</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(nsinteger point =<span style="color: #800080;"><span style="color: #800080;">0</span></span>; Pointcgfloat x = (cgfloat) point/<span style="color: #000000;"><span style="color: #000000;">(cgfloat) numofpoints;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span>CGFloat value = [tovalue Floatvalue]-d_value * (pow (m_e,-damping * x) * cos (velocity * x));<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">1 y = 1-e^{-5x} * cos (30x)</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span>values[point] =<span style="color: #000000;"><span style="color: #000000;">@ (value);</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;"></span> -</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">values;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> the</span>}</pre><p><p></p></p><p><p>next, Create an external class method and return a cakeyframeanimation:</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span>+ (cakeyframeanimation *) createspring: (nsstring *) keypath duration: (cftimeinterval) duration usingspringwithdamping :(cgfloat) damping initialspringvelocity: (cgfloat) Velocity fromvalue: (<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span>) Fromvalue Tovalue: (<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span><span style="color: #000000;"><span style="color: #000000;">) tovalue{</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span>Cakeyframeanimation *anim =<span style="color: #000000;"><span style="color: #000000;">[cakeyframeanimation animationwithkeypath:keypath];</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span>Nsmutablearray *values = [kyspringlayeranimation animationvalues:fromvalue tovalue:tovalue usingSpringWithDamping: Damping * Dampingfactor initialspringvelocity:velocity *<span style="color: #000000;"><span style="color: #000000;">Velocityfactor duration:duration];</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span>Anim.values =<span style="color: #000000;"><span style="color: #000000;">values;</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span>Anim.duration =<span style="color: #000000;"><span style="color: #000000;">duration;</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">anim;</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span>}</pre><p><p></p></p><p><p><strong>Another key</strong></p></p><p><p> above, We created the Cakeyframeanimation. But who do these values really work for? If you're familiar with coreanimation, yes, it works for incoming Keypath. And these keypath are actually the attribute @property in Calayer. For example, when an incoming keypath is transform.rotation.x, cakeyframeanimation causes the layer to rotate because cakeyframeanimation discovers that the attribute in Calayer is called transform, so the animation happened. Now what we need to change is the factor variable in theme one, so it's natural to think that we can give calayer a property named Factor on the line, This cakeyframeanimation added to the layer when the layer has this factor attribute, will be 60 frames different values assigned to Factor. Of course we have to control Fromvalue and Tovalue in 0~1: </p></p><pre><pre><span style="color: #008080;">1</span> cakeyframeanimation *anim = [kyspringlayeranimation createspring:<span style="color: #800000;">@ "</span><span style="color: #800000;">factor</span><span style="color: #800000;">"</span> Duration:<span style="color: #800080;">0.8</span> usingspringwithdamping:<span style="color: #800080;">0.5</span> initialspringvelocity:<span style="color: #800080;">3</span> fromvalue:@ (<span style="color: #800080;">1</span> ) tovalue:@ (<span style="color: #800080;">0</span><span style="color: #000000;">)];</span> <span style="color: #008080;">2</span> <span style="color: #800080;">0</span> <span style="color: #000000;">;</span> <span style="color: #008080;">3</span> [self addanimation:anim forkey:<span style="color: #800000;">@ "</span><span style="color: #800000;">restoreanimation</span><span style="color: #800000;">"</span>];</pre></pre><p><p></p></p><p><p>The last step, although cakeyframeanimation in real time to change the factor we want, but we also have to notify the screen refresh, so as to see the Animation.</p></p><pre><pre><span style="color: #008080;">1</span> + (BOOL) needsdisplayforkey: (nsstring *<span style="color: #000000;">) key{</span><span style="color: #008080;">2</span><span style="color: #0000ff;">if</span> ([key isequal:<span style="color: #800000;">@ "</span> <span style="color: #800000;"> Factor</span><span style="color: #800000;">"</span><span style="color: #000000;">]) {</span><span style="color: #008080;">3</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> YES;</span> <span style="color: #008080;">4</span> <span style="color: #000000;">}</span><span style="color: #008080;">5</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> [super needsdisplayforkey:key];</span> <span style="color: #008080;">6</span> }</pre></pre><p><p></p></p><p><p>The above code notifies the screen to refresh the screen in real time when the factor Changes.</p></p><p><p>finally, you need to reload Calayer-(id) initwithlayer: (gooeycircle *) layer method, in order to ensure that the animation can be coherent, you need to copy the previous state of the layer and all its properties.</p></p><pre><span style="color: #008080;"><span style="color: #008080;">1</span></span>-(<span style="color: #0000ff;"><span style="color: #0000ff;">ID</span></span>) Initwithlayer: (gooeycircle *<span style="color: #000000;"><span style="color: #000000;">) layer{</span></span><span style="color: #008080;"><span style="color: #008080;">2</span></span>Self =<span style="color: #000000;"><span style="color: #000000;">[super initwithlayer:layer];</span></span><span style="color: #008080;"><span style="color: #008080;">3</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(self) {</span></span><span style="color: #008080;"><span style="color: #008080;">4</span></span>Self.indicatorsize =<span style="color: #000000;"><span style="color: #000000;">layer.indicatorsize;</span></span><span style="color: #008080;"><span style="color: #008080;">5</span></span>Self.indicatorcolor =<span style="color: #000000;"><span style="color: #000000;">layer.indicatorcolor;</span></span><span style="color: #008080;"><span style="color: #008080;">6</span></span>Self.currentrect =<span style="color: #000000;"><span style="color: #000000;">layer.currentrect;</span></span><span style="color: #008080;"><span style="color: #008080;">7</span></span>Self.lastcontentoffset =<span style="color: #000000;"><span style="color: #000000;">layer.lastcontentoffset;</span></span><span style="color: #008080;"><span style="color: #008080;">8</span></span>Self.scrolldirection =<span style="color: #000000;"><span style="color: #000000;">layer.scrolldirection;</span></span><span style="color: #008080;"><span style="color: #008080;">9</span></span>Self.factor =<span style="color: #000000;"><span style="color: #000000;">layer.factor;</span></span><span style="color: #008080;"><span style="color: #008080;">Ten</span></span> <span style="color: #000000;"><span style="color: #000000;">}</span></span><span style="color: #008080;"><span style="color: #008080;"></span> one</span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;">self <span style="color: #000000;">;</span></span><span style="color: #008080;"><span style="color: #008080;"></span> a</span>}</pre><p><p></p></p><p><p><strong>Summarize:</strong></p></p><p><p>The key to making a custom animation is to have a variable, to have the Input. Like sliding scrollview, the distance of the slide is the input of the animation, it can be used as the animation variable, when there is no interaction, you can use the Caanimation. In fact, there is a timer at the bottom of the caanimation, and the function of the timer is to generate variables, time is a variable, you can produce changes in the input, you can see the state of change, even up is Animation.</p></p><p><p>iOS development-graphics programming oc & Sticky animations and jelly effects</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.