Sobes.tech
Junior — Middle

What are the differences between CoreGraphics and CoreAnimation, and in which cases is it preferable to use each technology?

sobes.tech AI

Answer from AI

CoreGraphics is a low-level 2D graphics library that allows drawing lines, shapes, text, and images directly in a graphics context. It works on the CPU and is used for creating static or dynamic raster images, such as custom drawing of UIViews.

CoreAnimation is a high-level animation technology that works on the GPU and allows creating smooth animations and layer transformations (CALayer). It is optimized for interface animation and visual effects management without manually redrawing content.

When to use:

  • CoreGraphics is suitable when you need to draw custom content (e.g., charts, diagrams, complex shapes) with pixel-level control.
  • CoreAnimation is better for animating layer properties (position, scale, opacity), creating transitions, and visual effects where performance and smoothness are important.

Example of using CoreAnimation for opacity animation:

let layer = CALayer()
layer.opacity = 0.0
let animation = CABasicAnimation(keyPath: "opacity")
animation.fromValue = 0.0
animation.toValue = 1.0
animation.duration = 1.0
layer.add(animation, forKey: "fadeIn")
layer.opacity = 1.0

Use CoreGraphics for precise drawing, and CoreAnimation for animations.