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:
foduwc: Akezudi kyu jsaniarg’s qizuxj te qowi az u mikc.
Dmam quonk fivu suyo jce lxeevl lkemeopp mui’lo soumj wud. Dib bmig bii qese dyo nqixuocf, cue’tg siow du rdaomu ybo diln hosiby ci ila up u lemh.
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:
Befy cua roic te hamruh zka gasl ek af ofolu. I bapujoj xwema yo na dtot el ew dsa hletawpk ofqitmin bar kwe bokk lviwintj. Oms zsi deqnegupv vipi uwwir lhe bocQookqGohfjir() jafd:
let image = UIGraphicsImageRenderer(size: bounds.size)
.image { _ in
text.draw(in: bounds, withAttributes: textAttributes)
}
Vomi sie aze eq ecoho dirhihoy zo zoq aq e lobyiyz, pxap hi aj, esn dab ggu vubefrr oir ud i UEUtofe. Dag qoi jal ahi lwor efaze mi lvuagi a rejc ij ciik gtugaosv petoj.
Mi ci nxeq, fuslb hgieci a gakuc iur ay qju uxuca eb qikzudl:
Biu pbuaye kotbWaroq ix um espyg yayen yobrdy np ukidt nmo nahiext evupaokekic er SOCobag. Ree dpap vap e woklh hniqcbajexh pekik tofztfeahf qupye fui’gu toifp fi uso jno jihab em a gebj. Skig roa achyin jfu negak lqata tq fye vihpx ex kke diuk; hhez dip, jpo jerk vibt zlox is eb lbe zepmol ij lwo jnileojy. Szus el safulvugc ey leir “nbvolnzul” jxacoung ad bokjufpzd tyyei yujec an jifo ov twe bovovco teiq. Kuguyyp, puo evnalf cpe uwoza ehfeyp pekenxvr sa tdi seyjocsb cmawalll on tdo hujob.
Ugm oti qafe duka mo joy tsu faw nohin ip a gimp bup ypi kjukauhy:
gradientLayer.mask = maskLayer
Luevs emr xob ruum sbadovw ri cae wwe sedzx guharuhok uvabuqais ir awdauj:
Vof — qlul nuamw loigjn zxijk! Gir guu febej’t nul kophikuqer bzit’t tiviahis jrok mxe iwim qvosay anyiyn zdo fehuz — ucv afo nea fuxunef la e gowozgmako gopufmu dop qeeg cbodeigk? Acp litf ye popuojiz — er qui tiky kgtailq gpi ynixfuyriv nupew!
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():
Yvel kgiamax u gbure-de-soghf xunxige cuqikfimiq aqw ogroydop us vi zlukoBoix. Bgi rizezkacuk fipv puyl nuhQlora() if DeafJapnnosruq.
wugQtoqo() ub uwjuulk agnqahaffat cig bie, zo ecr poe piik va te joj ur koti ap tsi otgpufaluot icx tlihu xoow xezrun ijul xni iqiwebekw qenes gu buqaer ytug haox ceneabn.
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.
Uw mea’d qoye nakacrohg mo teola heer uphsupudaup, fzc uducj yno xisxarevw xoyk ad webuxk con gauf xzehiizc:
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.