When do you need it? -Core capabilities of framework: - Contexts and Paths - Drawing lines, curves, and images - Applying transformations -Recipes and Examples Agenda What will I learn?
-Graphics API to produce lines, curves, transforms, gradients, etc. -Uses painters model for drawing operations What is Quartz 2D? Wasn’t this supposed to be about Core Graphics? Drawing Order Result
and caching - Easier to create and maintain - Keeps your graphics designers employed -It’s best to design your UI using images… When do I need it? Isn’t this what graphics designers are for? ...until you can’t
- Colors, line widths, transforms, etc. - It’s a state machine -Key Quartz Contexts: - CGContext - CGBitmapContext - CGPDFContext Quartz Graphics Contexts The Canvas
and AppKit -Get a reference to it: CGContext Drawing CGContext let context = UIGraphicsGetCurrentContext() iOS and tvOS let context = NSGraphicsContext.current()?.graphicsPort macOS
is the native UIKit color type -Use UIColor in almost all cases - Easier to create - Named color initializers - Easy to convert to and from CGColor Defining Colors CGColor vs UIColor // Convert to Quartz Type let cgColor = UIColor.red.cgColor context.setFillColor(cgColor) // Convert to UIKit Type let uiColor = UIColor(cgColor: cgColor)
by their endpoints -Basic Recipe: 1. Begin a new path 2. Move to initial starting point 3. Add lines defining shape 4. Close the path (optional) 5. Draw Path Drawing Lines Defining Paths
- Center x, y coordinates - Radius - Start and stop angles (in radians) - Direction (clockwise or counter-clockwise) -Created using: - addArc(center:radius:startAngle:endAngle:clockwise:) - addArc(tangent1End:tangent2End:radius:) Drawing Arcs Constructing Paths
Need to think upside down (or) - Transform coordinate system before using - UIBezierPath wraps Quartz GGPath functionality - Access to underlying path with CGPath property -Generally easier to use UIBezierPath on iOS and tvOS - * In Swift 3, direct Quartz usages is very similar UIBezierPath More iOS/tvOS-friendly solution
platform -Allow you to manipulate the coordinate system to translate, scale, skew, and rotate -Transform preserves collinearity of lines Affine Transformations Transforming the Coordinate System X
to another -Quartz supports two different gradient types - Linear varies between two endpoints - Radial varies between two radial endpoints Gradients Drawing Gradients Linear Radial
-Both functions require the following: - CGColorSpace - Colors (components or array of CGColor) - Array of CGFloat values defining gradient color stops Creating a CGGradient CGGradient
be drawn using: - draw(image:rect:) - draw(image:rect:byTiling) - CGBitmapContext allows for dynamically building image data Working with Images CGImage
- Easier to load and cache image content - Provides methods for drawing - Is aware flipped coordinate system - UIImage provides a CGImage property -Use CGImage for more advanced cases - Cropping, scaling, masking - Dynamic image creation CGImage vs UIImage Which should I use?
= CGRect(x: 20, y: 20, width: 200, height: 200) if let cgImage = albumImage.cgImage?.cropping(to: cropRect) { // Create new UIImage and use as needed let eddieImage = UIImage(cgImage: cgImage) }