Slide 1

Slide 1 text

How to tame Core Animation

Slide 2

Slide 2 text

tame adjective Reduced from a state of native wildness especially so as to be tractable and useful to humans

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

About me 15+ years of experience Swift developer Open source

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

a queue management system

Slide 7

Slide 7 text

Serving millions of people every month

Slide 8

Slide 8 text

Mr. Byte

Slide 9

Slide 9 text

Animations = easy?

Slide 10

Slide 10 text

– Soroush Khanlou “Animations in iOS are easy, except when they’re not.”

Slide 11

Slide 11 text

Intro to Core Animation Animation path property Real life example Agenda

Slide 12

Slide 12 text

SwiftUI

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

What is animation?

Slide 15

Slide 15 text

What is animation?

Slide 16

Slide 16 text

About Core Animation

Slide 17

Slide 17 text

➡ Layer Kit ➡ August 7, 2006 WWDC ➡ Mac OS X 10.5 Leopard ➡ Created by John Harper About Core Animation

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

Core animation

Slide 20

Slide 20 text

Core Animation provides high frame rates and smooth animations without burdening the CPU and slowing down your app. – Apple Documentation

Slide 21

Slide 21 text

NO. Is Core Animation == UIView animations ?

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Core Graphics

Slide 25

Slide 25 text

developer.apple.com

Slide 26

Slide 26 text

Core Animation ???

Slide 27

Slide 27 text

➡ iOS, macOS and tvOS ➡ Hardware accelerated GPU ➡ Runs on separate process Core Animation ???

Slide 28

Slide 28 text

Time of completion & frames

Slide 29

Slide 29 text

Animation Time Span New Value Existing Value frame.origin.x CALayers

Slide 30

Slide 30 text

0.25 seconds * 60 frames = 15 locations Interpolation example

Slide 31

Slide 31 text

Interpolation types

Slide 32

Slide 32 text

animation.timingFunction = CAMediaTimingFunction(name: .linear) // default // easeIn // easeInEaseOut // easeOut // linear

Slide 33

Slide 33 text

Animation types

Slide 34

Slide 34 text

Animation types ➡ Basic Animation ➡ Keyframe Animations ➡ Grouping Animations

Slide 35

Slide 35 text

setBounds CALayers

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

Layers vs Views

Slide 38

Slide 38 text

Views Layers vs complex, Auto Layout user interaction CPU powered restricted no responder chain drawn on the GPU

Slide 39

Slide 39 text

CAShapeLayer

Slide 40

Slide 40 text

let bezierPath = UIBezierPath() bezierPath.move(to: point1) bezierPath.addLine(to: point2) bezierPath.addLine(to: point3) bezierPath.addLine(to: point1) bezierPath.close() let shapeLayer = CAShapeLayer() shapeLayer.path = bezierPath.cgPath shapeLayer.lineWidth = 0 shapeLayer.fillColor = fillColor.cgColor self.layer.addSublayer(shapeLayer)

Slide 41

Slide 41 text

let bezierPath = UIBezierPath() bezierPath.move(to: point1) bezierPath.addLine(to: point2) bezierPath.addLine(to: point3) bezierPath.addLine(to: point1) bezierPath.close() let shapeLayer = CAShapeLayer() shapeLayer.path = bezierPath.cgPath shapeLayer.lineWidth = 0 shapeLayer.fillColor = fillColor.cgColor self.layer.addSublayer(shapeLayer)

Slide 42

Slide 42 text

let bezierPath = UIBezierPath() bezierPath.move(to: point1) bezierPath.addLine(to: point2) bezierPath.addLine(to: point3) bezierPath.addLine(to: point1) bezierPath.close() let shapeLayer = CAShapeLayer() shapeLayer.path = bezierPath.cgPath shapeLayer.lineWidth = 1 shapeLayer.fillColor = fillColor.cgColor self.layer.addSublayer(shapeLayer)

Slide 43

Slide 43 text

let bezierPath = UIBezierPath() bezierPath.move(to: point1) bezierPath.addLine(to: point2) bezierPath.addLine(to: point3) bezierPath.addLine(to: point1) bezierPath.close() let shapeLayer = CAShapeLayer() shapeLayer.path = bezierPath.cgPath shapeLayer.lineWidth = 1 shapeLayer.fillColor = fillColor.cgColor self.layer.addSublayer(shapeLayer)

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

let pathAnimation = CABasicAnimation(keyPath: #keyPath(CAShapeLayer.path)) shapeLayer.add(pathAnimation, forKey: "pathAnimation") pathAnimation.toValue = triangle.cgPath pathAnimation.duration = 1.0 pathAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut) pathAnimation.autoreverses = true pathAnimation.repeatCount = .greatestFiniteMagnitude

Slide 47

Slide 47 text

let pathAnimation = CABasicAnimation(keyPath: #keyPath(CAShapeLayer.path)) shapeLayer.add(pathAnimation, forKey: "pathAnimation") pathAnimation.toValue = triangle.cgPath pathAnimation.duration = 1.0 pathAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut) pathAnimation.autoreverses = true pathAnimation.repeatCount = .greatestFiniteMagnitude

Slide 48

Slide 48 text

let pathAnimation = CABasicAnimation(keyPath: #keyPath(CAShapeLayer.path)) shapeLayer.add(pathAnimation, forKey: "pathAnimation") pathAnimation.toValue = triangle.cgPath pathAnimation.duration = 1.0 pathAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut) pathAnimation.autoreverses = true pathAnimation.repeatCount = .greatestFiniteMagnitude

Slide 49

Slide 49 text

let pathAnimation = CABasicAnimation(keyPath: #keyPath(CAShapeLayer.path)) shapeLayer.add(pathAnimation, forKey: "pathAnimation") pathAnimation.toValue = triangle.cgPath pathAnimation.duration = 1.0 pathAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut) pathAnimation.autoreverses = true pathAnimation.repeatCount = .greatestFiniteMagnitude

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

let animationGroup = CAAnimationGroup() animationGroup.animations = [...] animationGroup.autoreverses = true animationGroup.repeatCount = .greatestFiniteMagnitude animationGroup.duration = 2.0

Slide 52

Slide 52 text

let animationGroup = CAAnimationGroup() animationGroup.animations = [...] animationGroup.autoreverses = true animationGroup.repeatCount = .greatestFiniteMagnitude animationGroup.duration = 2.0

Slide 53

Slide 53 text

let animationGroup = CAAnimationGroup() animationGroup.animations = [...] animationGroup.autoreverses = true animationGroup.repeatCount = .greatestFiniteMagnitude animationGroup.duration = 2.0

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

Snøhetta to build Shanghai Grand Opera House with spiral staircase roof

Slide 61

Slide 61 text

Cartesian Coordinates Polar Coordinates

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

fassko/animatingLavaTriangles

Slide 65

Slide 65 text

Experiment with animations

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

Know when it’s too much CA is powerful & well documented API Hard to do something out of bounds Conclusions

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

fassko www.kristaps.me