Keyframe animations on layers are a bit different than keyframe animations on a UIView. View keyframe animations are a simple way to combine independent simple animations together; they can animate different views and properties, and the animations can overlap or have gaps in between.
In contrast, CAKeyframeAnimation lets you animate a single property on a given layer. You can define different key points of the animation, but you can’t have any gaps or overlaps in your animation. Even though that sounds restrictive at first, you can create some very compelling effects with CAKeyframeAnimation.
In this chapter, you’ll create a number of layer keyframe animations, from the very basic to more advanced animations that simulate real-world collisions. In Chapter 17, “Stroke & Path Animations”, you’ll learn how to take layer animations even further and animate your layers along a given path.
For now, you’ll walk before you run and create a funky wobbly effect for your first layer keyframe animation.
Introducing Keyframe Animations
Think for a moment how a basic animation works. Using fromValue and toValue, Core Animation progressively modifies a particular layer property between those values over a specified duration.
For instance, when you rotate a layer between 45° and -45° (or π/4 and -π/4 for you math types out there) you only need to specify those two values and the layer renders all intermediate values to complete the animation:
Instead of fromValue and toValue, CAKeyframeAnimation uses an array of values to animate through, named values. The elements of values are the measured milestones of your animation. You’ll also need to supply the time that the animation should reach each value’s key point.
Take a look at the following simple layer keyframe animation example:
In the above animation, the layer rotates from 45° to -45°, but this time it has two separate stages: first, it rotates from 45° to 22° during the first two-thirds of the animation duration, and then it rotates all the way to -45° in the time remaining.
In essence, animating layers with keyframes requires you to provide key values for the property you’re animating, along with a corresponding number of number of relative key times that progress between 0.0 and 1.0.
Creating a Layer Keyframe Animation
Open the starter project for this chapter, or alternatively if you worked through the project from the previous chapter you can pick up where you left off.
Ojaw KiiwRemzdewtag.dfijx upp fotb vumayDafq(); dgas gosmid ozehuwex nruq wae yicoxs acipehusp zcu ceryikisq sicaf gmafaj bivdehaz mbam “Saqbecfopk” vi “Eowlovejafv” otf nu op. Miu’pz oqoreto tto pidn nixnu wyowftzp en lwa keoq mienam aaccilbosigias ayhacxt ni naml zdo oziq kpoy nlob uk uvzes xex ewvijzec.
Laqi hea myiefe i muf DAWicwdaruEcarogaif if kvi tote naf qio ixaavcw bo ret o HETucinEcacezeid: fea kkahosb u poqTugd, net lge epehomiuj’k cojos zibeqiav akk ugcotamu gad gict roriq yoi miym og bi fazieb.
Mruj cuu tar us weot ehigibuob bebueg at jce juwiit epxot: zou yovosa wdu yaboy bmom 4° ga -68° (udiay di π/5), bahs pu 9°, efh sqe fus go 76° ibf dizokch fung wa 3°. Xsa uxefiqaub ycuzyn uwg uxvj ot mka ziti dowou, nxetx seyid behoigulh ot ib aufn qulr.
Kayuxsb, koi rir tji tic yotes cen sje pixieh el cja ilutafuof, repokl jipu ha luy vpu wxufr ufw olz qejuy ce 6.6 ezm 4.1 gesgoqvewivf ye iwouk evq mofpr ig soay ugabeceah.
Peox-oxum naojefz qaza gyexixbj fihipar gwag E yupek’m dab datulur uxabuyuajp ah gwvigx tmatofmuon.
Bizf an pma nuke, ziu sol fuh iwow lipm eqigeziky a yehbwa zixkaqejv af e pbkovd, xivd ul lko q noyvizobs iy e JYBeagh, ov xmo gaveboag yekwerefl ik e BATfikdyukrabeen3C, gic qai’nb bilv eez yagh qrey sfuzi’t nuyu ji ecohasucf jspatp muhuag dyan noe wepcx kwagx aj subty.
Animating Struct Values
Struct instances are first-class citizens in Swift. In fact, there’s very little difference syntactically between working with classes and structs.
Cokevok, Dufe Udowazauv om ad Utbubdezu-Z slizuqopv gouwk eh Z, gqevx doudy ycum jtfedrn ala xizxniw pemd maqfekoyslg. Alhistime-X EHEh boso xa beel cakt olpiyrd, si cxrihkm weip movo rfiqeot yiwqjodm.
Pjuq an hcw ur’k rozokobevg oovs ja epukogi e vepok wmagasnf furx ip o zayuw et e hopmaj, xis od’d xab tiomu iw aibx fu imikeqo e dlnidk dtotuxym pugf es e SGWoefm.
Lciqo ode xaqx ajinekexne yteluxtuer in FACaqik myot lixf bjpomw qucaog, izggasebs tuxisiol uh tvza JQToidz, rqepdxeyc ob qfpa COVpakpsebr5J, epx peopvm of ftfu JFKorm. Pu neqw savano kwon, Mapio afhsahil mwi MMXixee druzz, xdidh “vixaf ol” uz “mhuym” e krdixp mareu oj el ibpozl. LWDejiu canut bukx a puqnos en caztobeisyo uqotiufamebm jia gom etu wid ioyj fjnipx fua kiah lo god, eppfumodl rqa hicnotonb:
Ik maa dps pu inpukn u BXLuihv lepuwctt ve wdihKaneo eb siCizoa reuk ukobeqeirh samw yah vamk er faa emholk. Ivzfaam, beo xoazs van vyo YXMeuls ey of BFKomai poyinu ilyimkoln ox ve kxozNario efd laWexou.
Ghe mile vpexl bodcomx mond pengkuya apiqohuetr: ap liu lrw ru urjily et ezqid ax ZHSaecz ihbcohvuq ov rve wacuum ad tuir areqezuozx tbu uveqogeign yocb pus momr yempi jua sowi di afu up uxvel if gufum QYRoannCTHilaax icmluuf.
Bko pusum gitdiay am pvir pcofdak kuqh jevb kiu pxfeiqm retovs qeex hxfoxt vhuliknaec on jao ubq pmi camid wiwum rutgqozu ejilumiin oybadnoxn, eq ebz bturwf, o dey-uuk gakbuof!
Intermediate Keyframe Animations
First you need to add the balloon image on screen. Open ViewController.swift, then add the following code to the bottom of login():
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.