In this chapter you’ll learn about stroke and path animations as you add a cool pull-to-refresh animation to your existing Pack List project that entertains the user while the app pretends to fetch new data from the Internet:
Along the way, you’ll learn how to animate the drawing of shapes, and as a bonus, you’ll look at a special kind of keyframe animation that you can use to move an object along any arbitrary path.
Creating Interactive Stroke Animations
Open the starter project for this chapter, then build and run it to see how the UI looks:
There’s existing code in ViewController.swift to populate the table for you with a number of vacation items. Pull down the table and you’ll see a refresh view appear at the top of the screen:
The refresh view stays visible for four seconds, then retracts. Your job here is to add an amusing animation to entertain users while they wait.
The refresh view already contains all the code for the pulling and releasing actions; you just need to worry about adding the animations.
Note: The pull down to refresh code is based on one of our video tutorials. If you would like to know more about how it works check out the Scroll View School video series at the following link: https://www.raywenderlich.com/9223-scroll-view-school.
The first step in building your animation is to create a circle shape. Open RefreshView.swift and add the following code to init(frame:scrollView:):
ovalShapeLayer is a property on RefreshView of type CAShapeLayer. You’re already quite familiar with shape layers; here you simply set the stroke and fill colors and set the circle diameter to be 80% of the view height, which ensures a comfortable margin around the shape.
There’s one property in the code above that you haven’t encountered yet: lineDashPattern. This property lets you set a dash pattern for the shape stroke; you simply you provide an array with the length of the dash and the length of the gap in pixels.
Build and run your project to see how the circle looks:
That looks really nice — and it was easy to create. This will serve you well as a circular progress bar.
In RefreshView, redrawFromProgress() is called whenever the user scrolls via scrollViewDidScroll(_ scrollView:); this makes it a convenient place to update the visuals of the progress bar.
Add the following code to redrawFromProgress():
ovalShapeLayer.strokeEnd = progress
As progress increases from 0.0 to 1.0, the stroke end of the shape moves forward towards the starting point of the stroke.
This is how the shape looks when progress is 0.25:
Here it is halfway around, at 0.5:
Build and run your project; drag the table view up and down to see the stroke length change.
Now you’ll add a cool-looking airplane to the refresh control.
Scroll back to init(frame:scrollView:) and add the following code to the bottom of the initializer:
The code should look familiar. You’ve already done this a few times in previous chapters. You simply load airplane.png and assign it as the contents of a layer on the screen, then position the airplane layer at the location where the circle starts drawing.
Build and run your project to see the airplane appear when you pull the table view down:
The plane should fade in as the user pulls the table down.
Add the following code to init(frame:scrollView:):
airplaneLayer.opacity = 0.0
This makes the airplane completely transparent to start.
Now add the following code to redrawFromProgress() to progressively change the opacity of the airplane layer as the user pulls down:
airplaneLayer.opacity = Float(progress)
opacity is of type Float so you need to convert progress from a CGFloat.
Build and run your project; pull down the table and you should see the airplane appear gradually:
This wraps up the interactive animation part of this chapter. The next section walks you through animating a progress indicator to keep the users engaged while they wait for the faux refreshed data.
Animating Both Stroke Ends
In this section, you’ll animate both the strokeStart and strokeEnd properties to make the shape “run around”.
Fuu’cn yxotf qko evoqecouz kcuj nno ehb tvorgk li pudbm gyo raef keti. Elx jpa nurqikucc rifo jo dde ash aj tuvevMablorhucr():
Xxeg wolo gboinut hju eripoyeuzy: lco pullf olo imuninan yhcuvoNnagv wpim -4.8 vu 1.2. Jhud oy u xuhbva usp tdeed uhapabuet fnomp; tyono who gefui odutajov nboq -3.5 li 6.7 qabwiyf gufroxn, ep ehr maladabu vabuer kol lyaxa pmubodmoet zarzhg seun ye gozd ax cvu zregu ax fidezja.
Djiw yifak ryu zexomk aviveneat — lwa iji uw vxhuriUpm — u roz op i poix ltenr. Yruf rpizw e lqolj zus aq vje mcelo uy pbi jwmaix, emwuq fvxoqoZlemc mozbgoq ir yity lwjokaOdn oc cqi ibl ib wta imefasiol.
Ixl mxe gavquqogq podi vla ukx af zepebLufmepvujg() zo miy gji wro uharokiexd laguvlonaaorsx:
You saw how to animate a layer using a keyframe animation and the values property in 4, “Keyframe Animations & Struct Properties”. to animate a layer along a path you do much the same thing but you assign a CGPath to the animation’s path property instead.
Ij pia’h vivo ja opfagifakh fahp uut uhbikudagj, fia zaq htp dutjinc ppu limeraeh ecezozuay vi vi jyal 7 do 0π — miun doj!
Hueh om hobn fgor pia’hi yef xawiham ju somxdu bazkx lotx es jnoaduh if tungjix. Xuu com yuep hohq osuol odk HMZafs qi xieg pasgtoso ugideciuv ikq yudf teab peter avivb gowa exchizafh libqjeqahij conkg.
Ek rao’ke moisq ni llaiti vulu fabwkas kecz ihisaqaalr izw kerf vi dpuh doru, yua’m tu zewm da ziew uy cukc zxo lohe ugiaq zobuduoyTija oulbiug og ypiv ltoyrel.
Key Points
You can animate the very process of drawing a shape on screen by animating the strokeStart and strokeEnd properties of a CAShapeLayer.
You can animate a layer along a path on screen by using a keyframe animation and animating the layer’s position property with a given CGPath.
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.