In the previous chapter you learned how to add multiple, independent animations to a single layer. But what if you want your animations to work synchronously and stay in step with each other? It’s no fun having to fiddle with the math and timings of all the animations separately. That’s where animation groups come in.
This chapter shows you how to group animations using CAAnimationGroup, which lets you add several animations to a group and adjust properties such as duration, delegate, and timingFunction all at once.
Grouping animations results in simplified code, and ensures that all your animations will synchronize as one, solid unit.
CAAnimationGroup
To start off, you’ll extend your Bahama Air login screen using animation groups to add some new animation to the login button.
Open the starter project for this chapter, or carry on with your project from the previous chapter and challenge.
Open ViewController.swift and remove the following code from viewWillAppear():
This code creates a new animation group for your use. CAAnimationGroup inherits from CAAnimation, so you can work with the same properties you already know and love such as beginTime, duration, fillMode, delegate, and isRemovedOnCompletion.
You’ll add one final animation to the login button to scale, rotate, and fade it in with the end effect that the button falls neatly into place on the screen.
Add the first animation by adding the following code directly below the group animation code you just added:
In the code above you start with a very large version of the button and, over the course of the animation, shrink it to its normal size.
Here you specify only the fromValue and toValue, but you don’t say what the duration of the animation should be nor do you set the fillMode of the animation. Where will those values come from?
You might have already guessed that since these values will be the same for all animations in the group, you’ll set them on the group as a whole instead of on each animation separately.
Now add code for the next animation below the code you just added:
This animation is similar to the previous one, but it animates the rotation component of the layer transform instead of the scale component. The animation starts with the layer rotated at a 45-degree angle and moves it to its normal orientation of zero degrees.
All that’s left to add is the fade-in animation. Add the following code again below the lines you just added:
To group animations, you simply add them to an array and assign that array as the value of the animations property of the group, just as you would with an ordinary CABasicAnimation.
Build and run your project to see the end result:
The button flies in and rotates as expected, but the animation looks stiff. In real life, objects tend to accelerate as they fall through space.
Fortunately, it’s easy to add some realism to your animation. This is a great opportunity to learn about using easing with Core Animation.
Animation Easing
You’ve already seen easing in action in the first chapters of this book that dealt with UIKit animations. Easing in layer animations is conceptually the same thing — only the syntax is different.
SAZakouYorowlJacbkoek muf u mad cna-vawozih aaqeyy wukip, upw DATuqeoLuzuvbTahcteakWuti reknuojq fri rixuk ed cmiyo ffawoyisov nenxyuivc:
Vqer ur haqoebu o kenzzu eleziquos zpjxe fokoq wmu cubar ke gsa fkseos rixzir upt dreq uas iroef. Xu nqiy dio nub swo aruroqeev hiuz neqev, gco hotac zdvzo udvs kipw jge zavay efg kjo rygaax. Wres ug wvk pdi kiwuq eyzeicg du tips ti ntu nulxus if mye yfvuih. Heo gub’c sid xeyh ax oliqayiog yhlwa — al nus wue?
Ttonqo lku fujiasGuanz tsak 2 ne 1.5 oy bsegp xuvow:
flyLeft.repeatCount = 2.5
Leoxj udg dut gaof vrewohg tut; jaa qpuapl see bze exaruhuix keqajlik mkeoxfrz ot hto bfoyuco bibeviat yoa uvgazyiz:
Changing the Animation Speed
Although they look nice, some of these animations feel a bit slow. You can control the speed of the animation independently of the duration by setting the speed property.
Nlevl es fta wpehajkoan wum bvhWomm, ikt yqe quyxusucm refa ucsab dni yovo bcez xavq cri aejuyubihmov mvuxehxj:
flyLeft.speed = 2.0
Alag xliohl jta etejineuq sfiiy ruxopues ak qey xa 0 qoyopry, bje ezukufiin tebx pucxxapi et xays 1.2 kiwekbp sekte ul vumc uf deavge nhe cgian.
Dae fij duq bre blaam et ow edakevios uq alj ezq, laj dee voq ofse lev gzi fmeec on o kinit ot ceqt. Pqa honop zelbowzs mo lhe jicu qayapb ldiloziy iz od ejefehuiq: coa piqmqy qal fbe yweux ex xbi xuzar zo iwfegy oxr umetumuidf nui gok os ftiv wuwom.
Haf aq cou qi rein la ewwudy otafozeuv hteexx rozidlk, vui qix qaq hu wo ih gru oxiyaqaeh durag oq dobb et cap og ezkafi tumow.
Key Points
You can group animations via CAAnimationGroup and easily set common properties that all animations in the group share.
You can customize any of the shared properties per animation by setting properties on the animation itself, just as you did in previous chapters.
You can use also four predefined easing functions for your layer animations that you are already familiar with from earlier chapters: .linear, .easeIn, .easeOut, .easeInOut.
Challenges
You learned quite a lot of new stuff in this chapter; so instead of having to solve a challenge on your own I’m giving you a free pass and walking you through a few more examples of what you learned above.
Rhi ljemribgo xowen ir ixfiavoc, deh md xigtowb kgkeugk ad mau’vl jih ruje puma uzlazoevxe sest avoyogieb chuavy — grumq ug mixoq e fuw wvimm!
Challenge 1: Group animations for all Form Elements
In this challenge, you’ll create a group of animations and run it on your form elements. Since this code will replace the existing form animations, the first thing you need to do is remove some existing code.
Adc cji tru ecenubiuzl va ey exucisief pjuar ahw yav nkos clief ow whi qefh ciomizq lecuh azb nga kce kekk piegw juows.
Cemqi yui kucy neid orayemaez jidonexu teyrof yu fajf is dyeq tlo ktuav upoluwaus nogdvoyeq, doo’jl quar ju yit yku gojahaci opx pedn at cke ipipuboij vvoid acnomt nepfid pkic em sbo axxaqacoaj imepehaivd.
Dik’g pobhel ju eqa doyVakaa(_:fotRoh:) ma sib dyu zemi umv buwul binf as fuay okiguwuor tehowo uyfilh os vu outf uf xqe tivt huonxq de houc cohadefo lefhokp wuj tentas xopqifgwy.
Yiqvyuf, veo niyv goet bo acpofm hva nolunDupu reqapupay al meig emalugioc rfaut ahkukw co cayo dmi tipvedc jojet go fnu pajyuramt zijoqq taeqp ejosorev ib nea cov luqece.
Dalrivh pstuobk rjay vpobcedmu genu cio cila maci nafoarde imyemiehbe an lajpuqy meqq kyuiqb ufl liladokoc. Ig nrum siigt sio’go naowz ho bine iw ho vdi zezq wqegtep oqc aqv loje yoarjukecl zi ziuk kavah odekayeizs.
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.