<span id="Label3"></p><p><p>On Nine chapters:</p></p> <ol> <ol> <li>Layer Tree</li> <li>Layer's Homestay Map</li> <li>Layer geometry</li> <li>Layer Visual effects</li> <li>Layer transformations</li> <li>Dedicated layers</li> <li>Implicit animations</li> <li>An explicit animation</li> <li>Layer Time</li> </ol> </ol><p><p>This essay mainly describes the layer Buffering.</p></p><p><p>The Core animation uses buffering to make the animation move smoother and more naturally.</p></p> <ul> <li><li>Animation speed: <ul> <li>The rate is calculated from the following formula: <ul> <li>Velocity = Change/time</li> <li>/*</li> <li>Assuming that the speed is constant throughout the animation, that is, linear-paced animation, a completely untrue effect</li> <li>*/</li> </ul></li> </ul></li></li> <li>Buffer function: <ul> <li><li>A function that uses a physical engine to model the friction and momentum of a moving object</li></li> <li><li>Camediatimingfunction: <ul> <li>A thing that simulates physical effects such as acceleration or deceleration to enhance realism by controlling animation buffering.</li> </ul></li></li> <li>Caanimation Timingfunction Property (camediatimingfunction type): <ul> <li><li> methods for creating instances of Camediatimingfunction types: <ul> <li> +timingfunctionwithname: </li> <li> receive parameters are one of the following constants: <ul> <li> Kcamedi Atimingfunctionlinear <ul> <li> Creates a linear timing function (such as a bullet bore), caanimation the Timingfunction property default function </li> </ul> </li> <l i> kcamediatimingfunctioneasein <ul> <li> Creates a method that slowly accelerates and then stops abruptly (for example, free fall) </li> </ul> <li> KCAMEDIATIMINGF Unctioneaseout <ul> <li> creates a full-speed start, and then slowly slows down (for example, the ball falls off the ground and bounces Up) </li> </ul> </li> <li> kcamediatimingfunctionease Ineaseout <ul> <li> Creates a process that slowly accelerates and slows down slowly (the default way to UIView Animations) </li> </ul> </li> <li> kcamediatimingfunctiondefaul T <ul> <li> creates a method similar to kcamediatimingfunctioneaseineaseout, but the acceleration and deceleration processes are slightly slower </li> </ul> </li> <li> Explicit Caanima tion It is not the default option </li> </l> </ul> </li> </ul> </li></li> <li><li>Demonstration Examples: <ul> <li>Create a red layer</li> <li>Self.colorlayer = [calayer layer];</li> <li>Self.colorLayer.frame = CGRectMake (0, 0, 100, 100);</li> <li>Self.colorLayer.position = Cgpointmake (self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0);</li> <li>Self.colorLayer.backgroundColor = [uicolor redcolor]. cgcolor;</li> <li>[self.view.layer addSublayer:self.colorLayer];</li> <li>-(void) touchesbegan: (nsset *) touches withevent: (uievent *) event{</li> <li>Configure the transaction</li> <li>[catransaction begin];</li> <li>[catransaction setanimationduration:1.0];</li> <li>[catransaction setanimationtimingfunction:[camediatimingfunction functionwithname:kcamediatimingfunctioneaseout] ];</li> <li>Set the position</li> <li>Self.colorLayer.position = [[touches anyobject] locationInView:self.view];</li> <li>Commit TRANSACTION</li> <li>[catransaction commit];</li> <li>}</li> </ul></li></li> </ul></li> </ul></li> <li>UIView Animation buffers: <ul> <ul> <li> To change the buffering options for UIView animations, add one of the following constants to the options parameter: </li> <li> uiviewanimationoptioncurveeaseinout </li> <li> Uiviewanimationoptioncurveeasein </li> <li> uiviewanimationoptioncurveeaseout </li> <li> Uiviewanimationoptioncurvelinear </li> <li> demonstration example: <ul> <li> self.colorview = [[UIView alloc] init]; </li> <li> self.colorView.bounds = cgrectmake (0, 0,.); </li> <li> self.colorView.center = cgpointmake (self.view.bounds.size.width/2, self.view.bounds.size.height/2); </li> <li> Self.colorView.backgroundColor = [uicolor redcolor]; </li> <li> [self.view addSubview:self.colorView]; </li> <li>-(void) touchesbegan: (nsset *) touches withevent: (uievent *) event{</li> <li>//perform the animation </li> <li> [UIV Iew animatewithduration:1.0 delay:0.0 options:uiviewanimationoptioncurveeaseout animations:^{</li> <li>//set the Position </li> <li> self.colorView.center = [[touches anyobject] locationInView:self.view]; </li> <li>} completion:null]; </li> <li>} </li> </ul> </li> </ul> </ul></li> <li>Buffering and Keyframe Animations: <ul> <li><li>Instead of applying a timing function to the entire animation process, this buffer is repeated for each Animation's process:</li></li> <li><li>Specific practices: <ul> <li>Set the Timingfunctions property (nsarray Type) of the keyframe animation cakeyframeanimation to specify a different timing function for each animation Step.</li> </ul></li></li> <li><li>Attention: <ul> <li>Specify that the number of functions must be equal to the number of elements of the keyframes array minus one, because it is a function that describes the speed of animation between each frame</li> </ul></li></li> <li>Demonstration Examples: <ul> <li><li>Create Sublayer</li></li> <li><li>Self.colorlayer = [calayer layer];</li></li> <li><li>Self.colorLayer.frame = CGRectMake (50.0f, 50.0f, 100.0f, 100.0f);</li></li> <li><li>Self.colorLayer.backgroundColor = [uicolor bluecolor]. cgcolor;</li></li> <li><li>Add it to our view</li></li> <li><li>[self.layerView.layer addSublayer:self.colorLayer];</li></li> <li><li>-(ibaction) changecolor{</li></li> <li><li>Create a keyframe animation</li></li> <li><li>Cakeyframeanimation *animation = [cakeyframeanimation animation];</li></li> <li><li>Animation.keypath = @ "backgroundcolor";</li></li> <li><li>Animation.duration = 2.0;</li></li> <li><li>Animation.values = @[</li></li> <li><li>(__bridge Id) [uicolor bluecolor]. cgcolor,</li></li> <li><li>(__bridge Id) [uicolor redcolor]. cgcolor,</li></li> <li><li>(__bridge Id) [uicolor greencolor]. cgcolor,</li></li> <li><li>(__bridge Id) [uicolor bluecolor]. cgcolor];</li></li> <li><li>Add Timing function</li></li> <li><li>Camediatimingfunction *FN = [camediatimingfunction functionwithname:kcamediatimingfunctioneasein];</li></li> <li><li>Animation.timingfunctions = @[fn, fn, fn];</li></li> <li><li>Apply animation to Layer</li></li> <li><li>[self.colorlayer addanimation:animation forkey:nil];</li></li> <li><li>}</li></li> </ul></li> </ul></li> <li><li>How the camediatimingfunction buffering function works: <ul> <li>The buffer function effect is reflected on a simple coordinate chart, the horizontal axis represents the time, and the longitudinal axes represent the amount of change</li> <li>Other words: <ul> <li>A linear buffer is a simple slash from the start point, which represents the speed</li> <li>But Camediatimingfunction used a function called a three-time Bezier curve,</li> <li>In other words, the speed line drawn by the camediatimingfunction buffer function on the coordinate chart is a Bezier curve, and the larger the tangent angle (the angle formed by the tangents and the timeline), the faster</li> <li>and a three-time Bezier curve needs to be defined by four points: <ul> <li>The first and last points represent the beginning and end of the Curve.</li> <li>The remaining two points are called control points because they control the shape of the Curve.</li> </ul></li> </ul></li> </ul></li></li> <li>Custom Buffering Functions: <ul> <li><li>Another constructor for Camediatimingfunction <ul> <li>+functionwithcontrolpoints::::</li> </ul></li></li> <li><li>Provides the creation of a custom buffer function</li></li> <li><li>One way to Camediatimingfunction: <ul> <li>-getcontrolpointatindex:values:</li> </ul></li></li> <li><li>To retrieve the point of the curve, you can find the point of the standard buffer function, and then use Uibezierpath and Cashapelayer to draw it Out.</li></li> <li>Demonstration Examples: <ul> <li><li>/*</li></li> <li><li>The standard camediatimingfunction buffering curve, the start and end points of the curve are always {0, 0}, and {1, 1}, so we only need to retrieve the second and third points of the curve (control points)</li></li> <li><li>*/</li></li> <li><li>Create timing function</li></li> <li><li>Camediatimingfunction *function = [camediatimingfunction functionwithname:kcamediatimingfunctioneaseout];</li></li> <li><li>Get control Points</li></li> <li><li>Cgpoint controlPoint1, controlPoint2;</li></li> <li><li>[function getcontrolpointatindex:1 values: (float *) &controlPoint1];</li></li> <li><li>[function getcontrolpointatindex:2 values: (float *) &controlPoint2];</li></li> <li><li>Create curve</li></li> <li><li>Uibezierpath *path = [[uibezierpath alloc] init];</li></li> <li><li>[path movetopoint:cgpointzero];</li></li> <li><li>[path Addcurvetopoint:cgpointmake (1, 1)</li></li> <li><li>Controlpoint1:controlpoint1 controlpoint2:controlpoint2];</li></li> <li><li>Scale the path up to a reasonable size for display</li></li> <li><li>[path Applytransform:cgaffinetransformmakescale (200, 200)];</li></li> <li><li>Create Shape Layer</li></li> <li><li>Cashapelayer *shapelayer = [cashapelayer layer];</li></li> <li><li>Shapelayer.strokecolor = [uicolor redcolor]. cgcolor;</li></li> <li><li>Shapelayer.fillcolor = [uicolor clearcolor]. cgcolor;</li></li> <li><li>Shapelayer.linewidth = 4.0f;</li></li> <li><li>Shapelayer.path = path. cgpath;</li></li> <li><li>[self.layerView.layer addsublayer:shapelayer];</li></li> <li><li>Flip geometry so 0,0 are in the Bottom-left</li></li> <li><li>self.layerView.layer.geometryFlipped = YES;</li></li> </ul></li> <li>Demonstration Examples: <ul> <li><li>/*</li></li> <li><li>Apply a custom buffer function</li></li> <li><li>*/</li></li> <li><li>-(void) setangle: (cgfloat) Angle forhand: (UIView *) handview animated: (BOOL) animated{</li></li> <li><li>Generate transform</li></li> <li><li>Catransform3d transform = catransform3dmakerotation (angle, 0, 0, 1);</li></li> <li><li>If (animated) {</li></li> <li><li>Create transform Animation</li></li> <li><li>Cabasicanimation *animation = [cabasicanimation animation];</li></li> <li><li>Animation.keypath = @ "transform";</li></li> <li><li>Animation.fromvalue = [handView.layer.presentationLayer valueforkey:@ "transform"];</li></li> <li><li>Animation.tovalue = [nsvalue valuewithcatransform3d:transform];</li></li> <li><li>Animation.duration = 0.5;</li></li> <li><li>Animation.delegate = self;</li></li> <li><li>Animation.timingfunction = [camediatimingfunction functionwithcontrolpoints:1:0:0.75:1];</li></li> <li><li>Apply Animation</li></li> <li><li>HandView.layer.transform = transform;</li></li> <li><li>[handview.layer addanimation:animation forkey:nil];</li></li> <li><li>} else {</li></li> <li><li>Set transform directly</li></li> <li><li>HandView.layer.transform = transform;</li></li> <li><li>}</li></li> <li><li>}</li></li> </ul></li> </ul></li> <li><li>More complex animation curves: <ul> <li>For example, a rubber ball falls to a hard ground scene, when it starts to fall, it will continue to accelerate know to fall to the ground, and then after a few bounces, and finally stopped</li> <li>This effect cannot be represented by a simple three-time Bezier curve, so it cannot be done with camediatimingfunction, and can be achieved in the following ways:</li> </ul></li></li> </ul><p style="margin-left: 90px;"><p style="margin-left: 90px;">1. Create an animation with cakeyframeanimation and split it into several steps, using your own timing function for each small step</p></p> <ul> <ul> <li style="list-style-type: none;"> <ul> <li>Or</li> </ul></li> </ul> </ul><p style="margin-left: 90px;"><p style="margin-left: 90px;">2. Use the timer to animate frames-by-frame updates</p></p> <ul> <li style="list-style-type: none;"> <ul> <li>here, we'll Explain 1: <ul> <li><li>Key-frame-based buffering:</li></li> <li>Use keyframes to achieve bounce animations: <ul> <li><li>1. Create a keyframe in the buffer curve for each significant point (the critical point is the peak of each bounce)</li></li> <li><li>2. Apply a buffer function to connect each segment of the curve</li></li> <li><li>/*</li></li> <li><li>We also need to specify the time offset for each keyframe by Keytimes.</li></li> <li><li>Because the time of each bounce is reduced, the keyframes are not evenly distributed</li></li> <li><li>*/</li></li> <li>Demonstration Examples: <ul> <li><li>UIImage *ballimage = [UIImage imagenamed:@ "ball.png"];</li></li> <li><li>Self.ballview = [[uiimageview alloc] initwithimage:ballimage];</li></li> <li><li>[self.containerview addSubview:self.ballView];</li></li> <li><li>Reset ball to top of screen</li></li> <li><li>Self.ballView.center = Cgpointmake (150, 32);</li></li> <li><li>Create Keyframe Animation</li></li> <li><li>Cakeyframeanimation *animation = [cakeyframeanimation animation];</li></li> <li><li>Animation.keypath = @ "position";</li></li> <li><li>Animation.duration = 1.0;</li></li> <li><li>Animation.delegate = self;</li></li> <li><li>Animation.values = @[</li></li> <li><li>[nsvalue Valuewithcgpoint:cgpointmake (150, 32)],</li></li> <li><li>[nsvalue Valuewithcgpoint:cgpointmake (150, 268)],</li></li> <li><li>[nsvalue Valuewithcgpoint:cgpointmake (150, 140)],</li></li> <li><li>[nsvalue Valuewithcgpoint:cgpointmake (150, 268)],</li></li> <li><li>[nsvalue Valuewithcgpoint:cgpointmake (150, 220)],</li></li> <li><li>[nsvalue Valuewithcgpoint:cgpointmake (150, 268)],</li></li> <li><li>[nsvalue Valuewithcgpoint:cgpointmake (150, 250)],</li></li> <li><li>[nsvalue Valuewithcgpoint:cgpointmake (150, 268)]</li></li> <li><li>];</li></li> <li><li>Animation.timingfunctions = @[</li></li> <li><li>[camediatimingfunction functionwithname:kcamediatimingfunctioneasein],</li></li> <li><li>[camediatimingfunction functionwithname:kcamediatimingfunctioneaseout],</li></li> <li><li>[camediatimingfunction functionwithname:kcamediatimingfunctioneasein],</li></li> <li><li>[camediatimingfunction functionwithname:kcamediatimingfunctioneaseout],</li></li> <li><li>[camediatimingfunction functionwithname:kcamediatimingfunctioneasein],</li></li> <li><li>[camediatimingfunction functionwithname:kcamediatimingfunctioneaseout],</li></li> <li><li>[camediatimingfunction functionwithname:kcamediatimingfunctioneasein]</li></li> <li><li>];</li></li> <li><li>Animation.keytimes = @[@0.0, @0.3, @0.5, @0.7, @0.8, @0.9, @0.95, @1.0];</li></li> <li><li>Apply Animation</li></li> <li><li>Self.ballView.layer.position = Cgpointmake (150, 268);</li></li> <li><li>[self.ballView.layer addanimation:animation forkey:nil];</li></li> </ul></li> </ul></li> </ul></li> </ul></li> </ul><p><p></p></p> <ul> <ul> <li style="list-style-type: none;"> <ul> <li style="list-style-type: none;"> <ul> <li style="list-style-type: none;"> <ul> <li>Process Automation: <ul> <li>Implement a method that uses a buffer function to convert any simple property animation into a keyframe animation:</li> <li>For automation, we need to know how to do the following two things:</li> </ul></li> </ul></li> </ul></li> </ul></li> </ul> </ul><p style="margin-left: 210px;"><p style="margin-left: 210px;">1. Automatically split any attribute animation into multiple keyframes</p></p><p style="margin-left: 210px;"><p style="margin-left: 210px;">2. Use a mathematical function to express the elastic animation, so that the frame can be made cheap</p></p> <ul> <li style="list-style-type: none;"> <ul> <li style="list-style-type: none;"> <ul> <li style="list-style-type: none;"> <ul> <li style="list-style-type: none;"> <ul> <li style="list-style-type: none;"><li style="list-style-type: none;"> <ul> <li>Resolve Issue 1: <ul> <li>Copying the interpolation mechanism of the core animation</li> <li>This is an incoming start and end point, and then a mechanism to specify a point in time between these two points to produce a new point.</li> <li>For simple floating-point start values, the formula is as follows (assuming a time from 0 to 1): <ul> <li>Value = (endvalue–startvalue) Xtime + startvalue;</li> </ul></li> <li>For values that are more complex types like cgpoint,cgcolorref or catransform3d:</li> <li>This method can be simply applied to each individual element (also the X and Y values in the cgpoint, the red, blue, green, transparent values in the cgcolorref, or the coordinates of the independent matrices in the Catransform3d)</li> <li>Once we can get arbitrary interpolation between the starting values of a property animation in code, we can split the animation into many independent keyframes and then produce a linear keyframe Animation.</li> </ul></li> </ul></li></li> <li>Example://create a keyframe animation using the inserted value <ul> <li><li>float interpolate (float from, float to, float Time) {</li></li> <li><li>Return (to-from) * time + from;</li></li> <li><li>}</li></li> <li><li>-(id) interpolatefromvalue: (id) fromvalue tovalue: (id) tovalue time: (float) time{</li></li> <li><li>If ([fromvalue iskindofclass:[nsvalue class]]) {</li></li> <li><li>Get type</li></li> <li><li>const char *type = [fromvalue objctype];</li></li> <li><li>If (strcmp (type, @encode (cgpoint)) = = 0) {</li></li> <li><li>Cgpoint from = [fromvalue cgpointvalue];</li></li> <li><li>Cgpoint to = [tovalue cgpointvalue];</li></li> <li><li>Cgpoint result = Cgpointmake (interpolate (from.x, to.x, time), interpolate (from.y, to.y, time));</li></li> <li><li>return [nsvalue valuewithcgpoint:result];</li></li> <li><li>}</li></li> <li><li>}</li></li> <li><li>Provide safe default implementation</li></li> <li><li>Return (time < 0.5)? fromvalue:tovalue;</li></li> <li><li>}</li></li> <li><li>-(void) animate{</li></li> <li><li>Reset ball to top of screen</li></li> <li><li>Self.ballView.center = Cgpointmake (150, 32);</li></li> <li><li>Set up animation parameters</li></li> <li><li>Nsvalue *fromvalue = [nsvalue valuewithcgpoint:cgpointmake (150, 32)];</li></li> <li><li>Nsvalue *tovalue = [nsvalue valuewithcgpoint:cgpointmake (150, 268)];</li></li> <li><li>Cftimeinterval Duration = 1.0;</li></li> <li><li>Generate KeyFrames</li></li> <li><li>Nsinteger numframes = Duration * 60;//total frames, 60 frames per second</li></li> <li><li>Nsmutablearray *frames = [nsmutablearray array];</li></li> <li><li>Get the insertion point for each frame</li></li> <li><li>for (int i = 0; i < numframes; i++) {</li></li> <li><li>Float time = 1/(float) Numframes * i;</li></li> <li><li>[frames addobject:[self interpolatefromvalue:fromvalue Tovalue:tovalue time:time]];</li></li> <li><li>}</li></li> <li><li>Create Keyframe Animation</li></li> <li><li>Cakeyframeanimation *animation = [cakeyframeanimation animation];</li></li> <li><li>Animation.keypath = @ "position";</li></li> <li><li>Animation.duration = 1.0;</li></li> <li><li>Animation.delegate = self;</li></li> <li><li>Animation.values = frames;</li></li> <li><li>Apply Animation</li></li> <li><li>[self.ballView.layer addanimation:animation forkey:nil];</li></li> <li><li>}</li></li> <li><li>/*</li></li> <li><li>What we've done so far is a very complicated way to replicate the behavior of cabasicanimation with linear buffering</li></li> <li><li>The advantage of this approach is that we can control the buffer more precisely, which means we can apply a fully customizable buffer function</li></li> <li><li>*/</li></li> </ul></li> <li>Example://implement a custom buffer function with KeyFrames <ul> <li><li>Float Bounceeaseout (float T) {</li></li> <li><li>If (t < 4/11.0) {</li></li> <li><li>return (121 * t * t)/16.0;</li></li> <li><li>} else if (t < 8/11.0) {</li></li> <li><li>return (363/40.0 * t * t)-(99/10.0 * t) + 17/5.0;</li></li> <li><li>} else if (t < 9/10.0) {</li></li> <li><li>return (4356/361.0 * t * t)-(35442/1805.0 * t) + 16061/1805.0;</li></li> <li><li>}</li></li> <li><li>return (54/5.0 * t * t)-(513/25.0 * t) + 268/25.0;</li></li> <li><li>}</li></li> <li><li>-(id) interpolatefromvalue: (id) fromvalue tovalue: (id) tovalue time: (float) time{</li></li> <li><li>If ([fromvalue iskindofclass:[nsvalue class]]) {</li></li> <li><li>Get type</li></li> <li><li>const char *type = [fromvalue objctype];</li></li> <li><li>If (strcmp (type, @encode (cgpoint)) = = 0) {</li></li> <li><li>Cgpoint from = [fromvalue cgpointvalue];</li></li> <li><li>Cgpoint to = [tovalue cgpointvalue];</li></li> <li><li>Cgpoint result = Cgpointmake (interpolate (from.x, to.x, time), interpolate (from.y, to.y, time));</li></li> <li><li>return [nsvalue valuewithcgpoint:result];</li></li> <li><li>}</li></li> <li><li>}</li></li> <li><li>Provide safe default implementation</li></li> <li><li>Return (time < 0.5)? fromvalue:tovalue;</li></li> <li><li>}</li></li> <li><li>-(void) animate{</li></li> <li><li>Reset ball to top of screen</li></li> <li><li>Self.ballView.center = Cgpointmake (150, 32);</li></li> <li><li>Set up animation parameters</li></li> <li><li>Nsvalue *fromvalue = [nsvalue valuewithcgpoint:cgpointmake (150, 32)];</li></li> <li><li>Nsvalue *tovalue = [nsvalue valuewithcgpoint:cgpointmake (150, 268)];</li></li> <li><li>Cftimeinterval Duration = 1.0;</li></li> <li><li>Generate KeyFrames</li></li> <li><li>Nsinteger numframes = Duration * 60;</li></li> <li><li>Nsmutablearray *frames = [nsmutablearray array];</li></li> <li><li>for (int i = 0; i < numframes; i++) {</li></li> <li><li>Float time = 1/(float) Numframes * i;</li></li> <li><li>Apply easing</li></li> <li><li>Time = Bounceeaseout (time);</li></li> <li><li>Add Keyframe</li></li> <li><li>[frames addobject:[self interpolatefromvalue:fromvalue Tovalue:tovalue time:time]];</li></li> <li><li>}</li></li> <li><li>Create Keyframe Animation</li></li> <li><li>Cakeyframeanimation *animation = [cakeyframeanimation animation];</li></li> <li><li>Animation.keypath = @ "position";</li></li> <li><li>Animation.duration = 1.0;</li></li> <li><li>Animation.delegate = self;</li></li> <li><li>Animation.values = frames;</li></li> <li><li>Apply Animation</li></li> <li><li>[self.ballView.layer addanimation:animation forkey:nil];</li></li> <li><li>}</li></li> </ul></li> </ul></li> </ul></li> </ul></li> </ul></li> </ul><p><p></p></p><p><p>IOS Core Animation Advanced techniques-buffering</p></p></span>
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