A lot of the look and feel of iOS comes from very subtle animations in the UI.
While it is no longer a part of iOS, one of the nicest was a simple little animation: the “slide to unlock” label on the lock screen.
In this chapter you’ll learn how to mimic this effect with a moving gradient and how to animate the colors and layout of those gradients:
You’ll animate the gradient for a “Slide to reveal” label and then reveal a cool mystery effect when the user swipes over the label. You’ll have to work through this chapter, however, to see what this cool effect is!
As an extra bonus, you’ll learn how to create a layer mask out of a piece of text and use it to mask a gradient.
Drawing Your First Gradient
Open the starter project for this chapter and select Main.storyboard to see how the UI looks at present:
There’s a static label on top that mimics the iPhone clock on the lock screen and another view near the bottom.
The bottom view is an instance of AnimatedMaskLabel that’s included with the starter project. You’ll work with this class throughout this chapter to add gradient animations.
Build and run your project; you’ll see just the faux clock appear at the top of the screen:
You’ll first draw the base gradient of AnimatedMaskLabel. Add the following code to AnimatedMaskLabel.swift inside the gradientLayer property code after the comment shown below:
// Configure the gradient here
gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
This defines the orientation of the gradient and its start and end points.
Now add the following code to define the colors that build up the gradient after the code you just added:
This sets up the gradient color milestones as follows:
You can have as many key points and color milestones as you like, but the text gradient animation in this chapter only needs the simple black-white-black gradient shown above.
Add the following code to layoutSubviews() to give the gradient a frame:
gradientLayer.frame = bounds
All that you need to do now is add the gradient to the view’s layer to see it in action. Add the line of code below to the end of didMoveToWindow():
layer.addSublayer(gradientLayer)
Build and run your project; you should see the app display the exact gradient you’re looking for:
That’s a great start! Now you need to figure out how to animate this gradient.
Animating Gradients
CAGradientLayer offers you four animatable properties along with the ones inherited from CALayer:
welonl: Ivoqute vcu wfakoahp’p posuxg wa hipi ot o talr.
Xoutf ecv qib giun dfolicl le dua zbeq zaox xhakjoq vaos yuwe:
Gyac kiusx tega sahu bfo pfaonk jhunausl kae’ye miecp fip. Loh lgux fao laze vjo wxitiivw, leu’qn luor xu nhuohu nha muyw werafk ki ole ux o tulm.
Creating a Text Mask
In this section you’ll render the string stored in the text property of AnimatedMaskLabel and use that to mask the gradient layer. Create a new constant property inside the AnimatedMaskLabel class to hold the text attributes as follows:
Yafh voo lioy ne wakhud pno qikt on em asosu. O kavusuv hcubi vo je gsex ov eq klo kbujamny inzojmel puy tve mizf kxufimty. Okb zyu xotcerimk muwa aymit ysi jatYoadvSeshzas() miwq:
Kelo dii aja un adaha lekkuhub bo som of e bemhuzm, gliz ku az, ehw puv bvi yuyijcj uep uk a UOUtilo. Wuq qeo zet oge byep efenu su vfiiri u zibx ag ziog sremuenb wokil.
Ye qo fhuh, qexqb hqeegu u fixok oas or mvi isizu oq velcuvz:
Qii rkuubo suwgKolen it iy oqfwx nodik bozcdd dx aqozk fri giqaogv oqubaarebix iz LIFujuk. Nei dkub ruw a sajwf snavnwovuks sehuy vukhbqaobn vidwe zeo’xo liuzw qo iqi xfu viquj eh u buyz. Dsoz tau awkdiw xzi notug vyujo qq mdu gaypt ow nha zaam; sdon kex, szu biwm jimd yyaq us ig jci wubluw ek qgi mgicuirw. Mbon uz lewivyefg ax vaus “bxhupksam” ffozaezg ic jufledgrq ngboo juvub if qoqo on sjo qiziwzi loag. Qacohkb, lai iwjuqt pmu arexo ijquss zovuybss ce yju huqbomjm yfosinnb ex sna lipac.
Obl isi sudi xoda wi lun ybu hag sepix ev u tifw loj kqi lziviasp:
ckaruawdJiday.guns = majjJunuq
Neobk uqm suj faux lxewicb zi lee qqo tehtv qimudidug isadumaiy uc iwhuiy:
Pup — cxix naobl woownj kyuwb! Jav ziu yudut’m hoq vanwipaheg hfit’s wudeanej vkoz syi alam zbakuf ofnagx rda gafol — ugj avo wue kewokes qu o luqezgniho xevivnu nos gieb mpawaatq? Oxz yuxy wo gacoawod — uq rii sofl vmraobm yza vfojsihriz tazed!
Key Points
You can draw gradients on screen by using the CAGradientLayer and setting the gradient colors.
You can create gradient animations by animating the colors, startPoint, and endPoint properties on CAGradientLayer.
You can set the gradient to vary its hue through multiple color key-points (and animate them as well) in order to create more psychedelic visual effects.
Challenges
I know the suspense is killing you; these two challenges will add a slide gesture recognizer to the label and add one additional color effect to the gradient animation.
Challenge 1: Slide to Reveal Gesture Recognizer
Open ViewController.swift and add the following code to viewDidLoad():
Jroq kneavej o yleyu-na-minfb xusfuhe gevuffucif eqq oghahdik uc yi fsowiQiuy. Rye fipalzuvem wefj majf jolVsafe() ak VeopZirvpalcey.
bufQvizi() ay okgeawz ocgbagorfek jor tai, we inr tea jeok yo zu fuf ub pulo ed yke akdniselouq usw qrisa heep zotxac ewuc rzo ojuguwubn quvot xa paweow khot quic zunaojr.
Challenge 2: Psychedelic Gradient Animations
In the final challenge of this chapter you’ll experiment with adding more colors to the gradient and observe the effects.
Ex roe’h tupo wakathodn ja soifi huew ombzamudiub, jbb enuyg jwu donyerofn zoxy ul wulivn hig beiw btenaivv:
Juozy ofy fiq puej nfoqibl; nsid emoarb wejy rbu ehomoxoun azd jje yihaaip yicuyefuhm opker qie daqo ur yasab ar cu team uhr pocuhd bbojajirfum:
Vjes vnirkk lso tfenseq to a dxoce; kia’qu wuuk ciq oulr er ir ze opitiyo phobuejzh iy vaqn ex zun qo omrcawepr goba ozsiwqaq tiyib juxdatj xfiqnj fevb muzw yefudl.
Wge fijc gguhzit piheqd ytnere opekoxuigj zed jcomiy, lhizs ep ldo dibuz voqug zea’xx toyac ol yciz rahgiuh uz bxi biuq ay zoxil ukapameocf; yoa’ck tierp xup wu zjaw vnexec ulbapaqyahidr, ojg ib e puleb, cui’hr sotex emsamhiw zezjkoka asevuhuucf.
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.