SlideShare a Scribd company logo
Core Graphic
      &
Core Animation

    Andreas Blick
    @aquarioverde
Agenda
  •       Introduction / Overview

  •       Core Graphics / Quartz2D

      •     Explanation

      •     Demo

  •       Core Animation

      •     Explanation

      •     Demo

CG & CA              19/11/2011 - Barcelona Develper Conference   @aquarioverde
Why?
  •       User Experience

      •     Extra polish

      •     Visual Feedback

  •       More human, more natural, real life

  •       User guiding

  •       Usability


CG & CA               19/11/2011 - Barcelona Develper Conference   @aquarioverde
Quartz2D

  •       Quartz2D forms part of the Core Graphics
          Framework

  •       They are often interchanged synonymously

  •       2D text and and graphics rendering library

  •       C based API



CG & CA              19/11/2011 - Barcelona Develper Conference   @aquarioverde
Architecture
                            UIKit


                    Core Animation


          Open GL                     Core Graphics


                             GPU


CG & CA     19/11/2011 - Barcelona Develper Conference   @aquarioverde
drawRect
  • To draw we need a graphics context
  • Lazy drawing
              - (void)drawRect:(CGRect)rect
              {
                  // Drawing code
              }


   [self setNeedsDisplay];
   [self setNeedsDisplayInRect:(CGRect)invalidRect;



CG & CA          19/11/2011 - Barcelona Develper Conference   @aquarioverde
Demo 1
                           Draw simple line

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGRect currentBounds = self.bounds;

    CGContextSetLineWidth(context, 3.0f);
    CGContextSetStrokeColorWithColor(context,
       [UIColor blueColor].CGColor);

    CGContextMoveToPoint(context, 0.0f, CGRectGetMidY(currentBounds));
    CGContextAddLineToPoint(context, CGRectGetMaxX(currentBounds),
       CGRectGetMidY(currentBounds));

    CGContextDrawPath(context, kCGPathStroke);
}



CG & CA               19/11/2011 - Barcelona Develper Conference   @aquarioverde
Coordinate Sytem
            UIView                                                      CG
(0,0)                                      x             y
            frame: (50,50, 75, 75)
            bounds: (0, 0, 75, 75)




  y                                               (0,0)                                  x
  CG & CA                  19/11/2011 - Barcelona Develper Conference    @aquarioverde
Drawing
  •       Drawing is procedural!

  •       Everything goes in order

  •       Use grahpic states

  •       CGPath vs CGContext


                   CGContextSaveGState(context);
                   CGContextRestoreGState(context);




CG & CA              19/11/2011 - Barcelona Develper Conference   @aquarioverde
Paths
  •       Defined shapes

      •     Lines

      •     Arcs

      •     (Bezier) Curves

      •     Rectangles

  •       Also used for

      •     Clipping

CG & CA                19/11/2011 - Barcelona Develper Conference   @aquarioverde
Drawing
  • Drawing modes:
   • Stroke, fill, ...
  • Line cap and join styles
   • Round, bevel
      CGContextBeginPath(context);
     ...
      CGContextClosePath(context);
      CGContextDrawPath(context, kCGPathStroke);


CG & CA           19/11/2011 - Barcelona Develper Conference   @aquarioverde
Context Types

  • CGPDFContextCreate
   • CGContextRelease
  • UIGraphicsBeginImageContext
   • UIImagePNGRepresentation

CG & CA      19/11/2011 - Barcelona Develper Conference   @aquarioverde
Demo 2
                    Draw a text with shadow



- (void)drawTextInRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetShadowWithColor(context, CGSizeMake(5.0, 2.0), 3.0,
       [UIColor grayColor].CGColor);

    [super drawTextInRect:rect];
}




CG & CA               19/11/2011 - Barcelona Develper Conference   @aquarioverde
Transformations

  •       Scaling

  •       Rotation

  •       Translation



            CGContextTranslateCTM(context, 0.0f, self.frame.size.height);
            CGContextScaleCTM(context, 1.0f, -1.0f);


CG & CA                  19/11/2011 - Barcelona Develper Conference   @aquarioverde
Text Drawing
  •       Using Quartz

      •     Fast but restricted to MacRoman character set

  •       UIKit Alternative

      •     Very slow

      •     Uses UIKit coordinate system!

           [myString drawAtPoint:CGPointMake(0.0, 0.0)
             withFont:[UIFont systemFontOfSize:12.0]];


CG & CA              19/11/2011 - Barcelona Develper Conference   @aquarioverde
Demo 3
                     Draw a gradient button

CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();

CGGradientRef buttonGradient = CGGradientCreateWithColorComponents
   (rgbColorSpace, components, locations, nrOfLocations);

CGRect buttonBounds = self.bounds;
CGPoint gradientStart = CGPointMake(CGRectGetMidX(buttonBounds), 0.0f);
CGPoint gradientEnd = CGPointMake(CGRectGetMidX(buttonBounds),
   CGRectGetMidY(buttonBounds));

CGContextDrawLinearGradient(context, buttonGradient, gradientStart,
   gradientEnd, 0);




CG & CA               19/11/2011 - Barcelona Develper Conference   @aquarioverde
Pixel aligned drawing
  •       User space vs device space

      •     Drawing uses points instead of pixels!

      •     Float coordinates

  •       Uses half pixel boundary

      •     Path inbetween pixels

      •     Offset half a pixel if necessary


CG & CA               19/11/2011 - Barcelona Develper Conference   @aquarioverde
Pixel aligned drawing




CG & CA   19/11/2011 - Barcelona Develper Conference   @aquarioverde
Pixel aligned drawing
          (2.0, 0.0)




CG & CA                19/11/2011 - Barcelona Develper Conference   @aquarioverde
Pixel aligned drawing
          (2.0, 0.0)




CG & CA                19/11/2011 - Barcelona Develper Conference   @aquarioverde
Pixel aligned drawing
          (2.0, 0.0)




CG & CA                19/11/2011 - Barcelona Develper Conference   @aquarioverde
Pixel aligned drawing
          (2.0, 0.0)   (4.5, 0.0)




CG & CA                       19/11/2011 - Barcelona Develper Conference   @aquarioverde
Pixel aligned drawing
          (2.0, 0.0)   (4.5, 0.0)




CG & CA                       19/11/2011 - Barcelona Develper Conference   @aquarioverde
Pixel aligned drawing
          (2.0, 0.0)   (4.5, 0.0)




CG & CA                       19/11/2011 - Barcelona Develper Conference   @aquarioverde
Pixel aligned drawing
          (2.0, 0.0)   (4.5, 0.0)




CG & CA                       19/11/2011 - Barcelona Develper Conference   @aquarioverde
Pixel aligned drawing
          (2.0, 0.0)   (4.5, 0.0)




                                                                  Fill




CG & CA                       19/11/2011 - Barcelona Develper Conference   @aquarioverde
References
  • Quartz2D Programming Guide
  • Core Graphics Framework Reference
  • Cocoa Drawing Guide
  • Drawing and Printing Guide for iOS
  • UIKit Function Reference
  • NSString (UIKit Additions)
CG & CA      19/11/2011 - Barcelona Develper Conference   @aquarioverde
Core Animation

  •       Objective C framework for graphics rendering,
          proyection and animation

  •       Behind the scenes of any UIView

  •       Executed on GPU

  •       Done in background thread



CG & CA              19/11/2011 - Barcelona Develper Conference   @aquarioverde
UIView Animation

  • Core Animation behind the scenes
  • Handles all the math
  • Animatable properties:
   • center, frame, bounds, color, opacity, ...
  • Blocks vs. pre iOS4
CG & CA        19/11/2011 - Barcelona Develper Conference   @aquarioverde
Pre iOS 4

  •       Animate everything that is between begin and
          commit methods

  •       Duration, animation curve

  •       Repeat count, repeat autoreverse

  •       Delegate to chain animations



CG & CA              19/11/2011 - Barcelona Develper Conference   @aquarioverde
Demo 4
                  Pre iOS 4 about view appear


[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

self.aboutView.alpha = 1.0;
self.aboutView.frame = finalFrame;

[UIView commitAnimations];




CG & CA               19/11/2011 - Barcelona Develper Conference   @aquarioverde
Blocks


  •       One line of code (even though a long line)

  •       No delegates/callbacks necessary

  •       Synchronized




CG & CA              19/11/2011 - Barcelona Develper Conference   @aquarioverde
UIView Transitions

  •       Animations to execute when switching views

  •       Always need a container view

  •       They have a meaning: HIG

  •       Differ from iPad to iPhone

  •       ModalViewController



CG & CA              19/11/2011 - Barcelona Develper Conference   @aquarioverde
Demo 6
                         iPad CurlUp Block



[UIView transitionFromView:(self.aboutView)
       toView:(self.venueView)
       duration:1.0
       options:(UIViewAnimationOptionTransitionCurlDown |
       UIViewAnimationOptionShowHideTransitionViews)
       completion:^(BOOL finished) {
       }];




CG & CA               19/11/2011 - Barcelona Develper Conference   @aquarioverde
CALayer Animations

  • Every UIView is backed by a CALayer
   • QuartzCore framework
  • No touch support
  • Slightly higher performance on iOS
  • Same on MacOS as on iOS
CG & CA      19/11/2011 - Barcelona Develper Conference   @aquarioverde
Layer geometry
          (0,0)                                               x

                                                                     bounds

          sublayer



                                                              position:
                                                              anchorPoint (0.5, 0.5)


               y
CG & CA          19/11/2011 - Barcelona Develper Conference           @aquarioverde
Layer geometry




CG & CA     19/11/2011 - Barcelona Develper Conference   @aquarioverde
Layer geometry




CG & CA     19/11/2011 - Barcelona Develper Conference   @aquarioverde
Layer geometry




CG & CA     19/11/2011 - Barcelona Develper Conference   @aquarioverde
Layer Types
  •       CAEAGLLayer

  •       CATiledLayer

  •       CAScrollLayer

  •       CAReplicatorLayer

  •       CAShapeLayer

  •       CAGradientLayer


CG & CA             19/11/2011 - Barcelona Develper Conference   @aquarioverde
CALayer
  • Key-value compliant
  • Drawing
   • Don’t subclass
   • Use delegates instead
  • Every layer has a presentation layer
   • Hit testing to get current position
CG & CA       19/11/2011 - Barcelona Develper Conference   @aquarioverde
Implicit Animations
  •       Property changes

  •       Animates automatically

      •     1/4 second

      •     linear

  •       Exception: UIView layer

  •       Animation Transaction

      •     begin, commit

CG & CA              19/11/2011 - Barcelona Develper Conference   @aquarioverde
Explicit Animation
  •       CABasicAnimation

  •       CAKeyframeAnimation

      •     Define animation points and timing

      •     Keypath animation

           •   Animate a layer along a drawn path

  •       CAAnimationGroup


CG & CA              19/11/2011 - Barcelona Develper Conference   @aquarioverde
Explicit Animation
  •       CAMediaTimingFunction

  •       Animation chaining: delegate

      •     animationDidStop:finished:

      •     animationDidStart:

  •       CATransform3D

      •     scale, rotate, translate


CG & CA               19/11/2011 - Barcelona Develper Conference   @aquarioverde
Demo 7
                  Custom property animation

CABasicAnimation *iconAnimation =
   [CABasicAnimation animationWithKeyPath:@"position"];

iconAnimation.delegate = self;
iconAnimation.duration = 2.0;
iconAnimation.timingFunction = [CAMediaTimingFunction functionWithName:
   kCAMediaTimingFunctionEaseInEaseOut];

iconAnimation.removedOnCompletion = NO;
iconAnimation.fillMode = kCAFillModeForwards;

iconAnimation.toValue = [NSValue valueWithCGPoint:topIcon.layer.position];

[theAboutIcon.layer addAnimation:iconAnimation forKey:@"moveBelow"];




CG & CA               19/11/2011 - Barcelona Develper Conference   @aquarioverde
References

  • Core Animation Programming Guide
  • Animation Basics
  • UIView Programming Guide - Animations

CG & CA      19/11/2011 - Barcelona Develper Conference   @aquarioverde
Thank You!


 @aquarioverde

More Related Content

PDF
Models for hierarchical data
PPT
Design Pattern
PDF
스프링 부트와 로깅
PPTX
Let us understand design pattern
PPTX
Python: Common Design Patterns
PPTX
Coding standards for java
PPTX
Prototype model 130714101556-phpapp02
Models for hierarchical data
Design Pattern
스프링 부트와 로깅
Let us understand design pattern
Python: Common Design Patterns
Coding standards for java
Prototype model 130714101556-phpapp02

What's hot (20)

PPTX
Software Engineering concept
PPTX
Design Pattern - Factory Method Pattern
PPTX
Gof design patterns
PPTX
Solid principles
KEY
Implementing CATiledLayer
PPTX
Software Evolution
PPTX
Design pattern-presentation
PDF
Design patterns
PDF
Design patterns tutorials
PDF
Real-Time Stream Processing with KSQL and Apache Kafka
PDF
Microservice architecture
PPT
Introduction to Software Engineering
PPTX
Software Engineering Methodologies
PDF
Software evolution and maintenance
PDF
DDD Introduction
PDF
Explicit architecture
PPTX
Integration testing
PPT
Design Patterns
PPT
Software Requirements engineering
PPT
Software requirements engineering lecture 01
Software Engineering concept
Design Pattern - Factory Method Pattern
Gof design patterns
Solid principles
Implementing CATiledLayer
Software Evolution
Design pattern-presentation
Design patterns
Design patterns tutorials
Real-Time Stream Processing with KSQL and Apache Kafka
Microservice architecture
Introduction to Software Engineering
Software Engineering Methodologies
Software evolution and maintenance
DDD Introduction
Explicit architecture
Integration testing
Design Patterns
Software Requirements engineering
Software requirements engineering lecture 01
Ad

Viewers also liked (20)

PDF
Core Animation
PPTX
Graphics Libraries
PDF
Drawing with Quartz on iOS
PPTX
try! Swift - Advanced Graphics with Core Animation
PDF
Starting Core Animation
PDF
Creating Container View Controllers
PDF
Building Modern Audio Apps with AVAudioEngine
PDF
Mastering Media with AV Foundation
PPTX
Basketball
PPTX
Jocelyn power point
PPT
Rocking slideshow
PDF
احترف صيانة الكمبيوتر
PPT
If then vb2010
PPT
الإنترنت في خدمة الباحثين
PDF
Master Video with AV Foundation
PPT
تصميم مواقع
PDF
Composing and Editing Media with AV Foundation
KEY
Animation in iOS
PPTX
20 iOS developer interview questions
PPTX
iOS Developer Interview Questions
Core Animation
Graphics Libraries
Drawing with Quartz on iOS
try! Swift - Advanced Graphics with Core Animation
Starting Core Animation
Creating Container View Controllers
Building Modern Audio Apps with AVAudioEngine
Mastering Media with AV Foundation
Basketball
Jocelyn power point
Rocking slideshow
احترف صيانة الكمبيوتر
If then vb2010
الإنترنت في خدمة الباحثين
Master Video with AV Foundation
تصميم مواقع
Composing and Editing Media with AV Foundation
Animation in iOS
20 iOS developer interview questions
iOS Developer Interview Questions
Ad

Similar to Core Graphics & Core Animation (20)

KEY
Getting Started with CoreGraphics
PDF
아이폰강의(7) pdf
PDF
iOS Views
PDF
Hi performance table views with QuartzCore and CoreText
PDF
Quartz 2D with Swift 3
PDF
Advanced Imaging on iOS
PDF
05 Views
PDF
The Dark Depths of iOS [CodeMash 2011]
PDF
The Great CALayer Tour
PDF
Core Image: The Most Fun API You're Not Using (CocoaConf Columbus 2014)
PDF
Core Image
PDF
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
PDF
Power of canvas
PDF
Intro to HTML5 Game Programming
PDF
Understanding Hardware Acceleration on Mobile Browsers
PPTX
Layer architecture of ios (1)
PDF
Introduction to Canvas and Native Web Vector Graphics
PPTX
HTML5 and Other Modern Browser Game Tech
PDF
iOSToolkit
PPT
HTML5 Canvas
Getting Started with CoreGraphics
아이폰강의(7) pdf
iOS Views
Hi performance table views with QuartzCore and CoreText
Quartz 2D with Swift 3
Advanced Imaging on iOS
05 Views
The Dark Depths of iOS [CodeMash 2011]
The Great CALayer Tour
Core Image: The Most Fun API You're Not Using (CocoaConf Columbus 2014)
Core Image
Core Image: The Most Fun API You're Not Using, CocoaConf Atlanta, December 2014
Power of canvas
Intro to HTML5 Game Programming
Understanding Hardware Acceleration on Mobile Browsers
Layer architecture of ios (1)
Introduction to Canvas and Native Web Vector Graphics
HTML5 and Other Modern Browser Game Tech
iOSToolkit
HTML5 Canvas

Recently uploaded (20)

PDF
KodekX | Application Modernization Development
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Spectroscopy.pptx food analysis technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Approach and Philosophy of On baking technology
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KodekX | Application Modernization Development
Review of recent advances in non-invasive hemoglobin estimation
sap open course for s4hana steps from ECC to s4
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Spectroscopy.pptx food analysis technology
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced methodologies resolving dimensionality complications for autism neur...
Big Data Technologies - Introduction.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
MYSQL Presentation for SQL database connectivity
Per capita expenditure prediction using model stacking based on satellite ima...
Approach and Philosophy of On baking technology
Network Security Unit 5.pdf for BCA BBA.
Mobile App Security Testing_ A Comprehensive Guide.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation_ Review paper, used for researhc scholars
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf

Core Graphics & Core Animation

  • 1. Core Graphic & Core Animation Andreas Blick @aquarioverde
  • 2. Agenda • Introduction / Overview • Core Graphics / Quartz2D • Explanation • Demo • Core Animation • Explanation • Demo CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 3. Why? • User Experience • Extra polish • Visual Feedback • More human, more natural, real life • User guiding • Usability CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 4. Quartz2D • Quartz2D forms part of the Core Graphics Framework • They are often interchanged synonymously • 2D text and and graphics rendering library • C based API CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 5. Architecture UIKit Core Animation Open GL Core Graphics GPU CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 6. drawRect • To draw we need a graphics context • Lazy drawing - (void)drawRect:(CGRect)rect { // Drawing code } [self setNeedsDisplay]; [self setNeedsDisplayInRect:(CGRect)invalidRect; CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 7. Demo 1 Draw simple line - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGRect currentBounds = self.bounds; CGContextSetLineWidth(context, 3.0f); CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); CGContextMoveToPoint(context, 0.0f, CGRectGetMidY(currentBounds)); CGContextAddLineToPoint(context, CGRectGetMaxX(currentBounds), CGRectGetMidY(currentBounds)); CGContextDrawPath(context, kCGPathStroke); } CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 8. Coordinate Sytem UIView CG (0,0) x y frame: (50,50, 75, 75) bounds: (0, 0, 75, 75) y (0,0) x CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 9. Drawing • Drawing is procedural! • Everything goes in order • Use grahpic states • CGPath vs CGContext CGContextSaveGState(context); CGContextRestoreGState(context); CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 10. Paths • Defined shapes • Lines • Arcs • (Bezier) Curves • Rectangles • Also used for • Clipping CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 11. Drawing • Drawing modes: • Stroke, fill, ... • Line cap and join styles • Round, bevel CGContextBeginPath(context); ... CGContextClosePath(context); CGContextDrawPath(context, kCGPathStroke); CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 12. Context Types • CGPDFContextCreate • CGContextRelease • UIGraphicsBeginImageContext • UIImagePNGRepresentation CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 13. Demo 2 Draw a text with shadow - (void)drawTextInRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetShadowWithColor(context, CGSizeMake(5.0, 2.0), 3.0, [UIColor grayColor].CGColor); [super drawTextInRect:rect]; } CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 14. Transformations • Scaling • Rotation • Translation CGContextTranslateCTM(context, 0.0f, self.frame.size.height); CGContextScaleCTM(context, 1.0f, -1.0f); CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 15. Text Drawing • Using Quartz • Fast but restricted to MacRoman character set • UIKit Alternative • Very slow • Uses UIKit coordinate system! [myString drawAtPoint:CGPointMake(0.0, 0.0) withFont:[UIFont systemFontOfSize:12.0]]; CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 16. Demo 3 Draw a gradient button CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); CGGradientRef buttonGradient = CGGradientCreateWithColorComponents (rgbColorSpace, components, locations, nrOfLocations); CGRect buttonBounds = self.bounds; CGPoint gradientStart = CGPointMake(CGRectGetMidX(buttonBounds), 0.0f); CGPoint gradientEnd = CGPointMake(CGRectGetMidX(buttonBounds), CGRectGetMidY(buttonBounds)); CGContextDrawLinearGradient(context, buttonGradient, gradientStart, gradientEnd, 0); CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 17. Pixel aligned drawing • User space vs device space • Drawing uses points instead of pixels! • Float coordinates • Uses half pixel boundary • Path inbetween pixels • Offset half a pixel if necessary CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 18. Pixel aligned drawing CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 19. Pixel aligned drawing (2.0, 0.0) CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 20. Pixel aligned drawing (2.0, 0.0) CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 21. Pixel aligned drawing (2.0, 0.0) CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 22. Pixel aligned drawing (2.0, 0.0) (4.5, 0.0) CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 23. Pixel aligned drawing (2.0, 0.0) (4.5, 0.0) CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 24. Pixel aligned drawing (2.0, 0.0) (4.5, 0.0) CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 25. Pixel aligned drawing (2.0, 0.0) (4.5, 0.0) CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 26. Pixel aligned drawing (2.0, 0.0) (4.5, 0.0) Fill CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 27. References • Quartz2D Programming Guide • Core Graphics Framework Reference • Cocoa Drawing Guide • Drawing and Printing Guide for iOS • UIKit Function Reference • NSString (UIKit Additions) CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 28. Core Animation • Objective C framework for graphics rendering, proyection and animation • Behind the scenes of any UIView • Executed on GPU • Done in background thread CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 29. UIView Animation • Core Animation behind the scenes • Handles all the math • Animatable properties: • center, frame, bounds, color, opacity, ... • Blocks vs. pre iOS4 CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 30. Pre iOS 4 • Animate everything that is between begin and commit methods • Duration, animation curve • Repeat count, repeat autoreverse • Delegate to chain animations CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 31. Demo 4 Pre iOS 4 about view appear [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:2.0]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; self.aboutView.alpha = 1.0; self.aboutView.frame = finalFrame; [UIView commitAnimations]; CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 32. Blocks • One line of code (even though a long line) • No delegates/callbacks necessary • Synchronized CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 33. UIView Transitions • Animations to execute when switching views • Always need a container view • They have a meaning: HIG • Differ from iPad to iPhone • ModalViewController CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 34. Demo 6 iPad CurlUp Block [UIView transitionFromView:(self.aboutView) toView:(self.venueView) duration:1.0 options:(UIViewAnimationOptionTransitionCurlDown | UIViewAnimationOptionShowHideTransitionViews) completion:^(BOOL finished) { }]; CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 35. CALayer Animations • Every UIView is backed by a CALayer • QuartzCore framework • No touch support • Slightly higher performance on iOS • Same on MacOS as on iOS CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 36. Layer geometry (0,0) x bounds sublayer position: anchorPoint (0.5, 0.5) y CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 37. Layer geometry CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 38. Layer geometry CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 39. Layer geometry CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 40. Layer Types • CAEAGLLayer • CATiledLayer • CAScrollLayer • CAReplicatorLayer • CAShapeLayer • CAGradientLayer CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 41. CALayer • Key-value compliant • Drawing • Don’t subclass • Use delegates instead • Every layer has a presentation layer • Hit testing to get current position CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 42. Implicit Animations • Property changes • Animates automatically • 1/4 second • linear • Exception: UIView layer • Animation Transaction • begin, commit CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 43. Explicit Animation • CABasicAnimation • CAKeyframeAnimation • Define animation points and timing • Keypath animation • Animate a layer along a drawn path • CAAnimationGroup CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 44. Explicit Animation • CAMediaTimingFunction • Animation chaining: delegate • animationDidStop:finished: • animationDidStart: • CATransform3D • scale, rotate, translate CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 45. Demo 7 Custom property animation CABasicAnimation *iconAnimation = [CABasicAnimation animationWithKeyPath:@"position"]; iconAnimation.delegate = self; iconAnimation.duration = 2.0; iconAnimation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]; iconAnimation.removedOnCompletion = NO; iconAnimation.fillMode = kCAFillModeForwards; iconAnimation.toValue = [NSValue valueWithCGPoint:topIcon.layer.position]; [theAboutIcon.layer addAnimation:iconAnimation forKey:@"moveBelow"]; CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde
  • 46. References • Core Animation Programming Guide • Animation Basics • UIView Programming Guide - Animations CG & CA 19/11/2011 - Barcelona Develper Conference @aquarioverde

Editor's Notes

  • #2: \n
  • #3: \n
  • #4: draw custom user elements\nbeautiful details\nnot just cool effects\nguides the eye\neasy to use\n\n
  • #5: C based API: needs more coding -> create proper library\nonce you understand the basics its pretty easy\na lot of copy and paste ;-)\n
  • #6: \n
  • #7: graphics context: canvas on which we do our 2D drawing\nthe only place where we can get this context is in our drawRect method\ncontext is set up automatically!\ncalled by the system when needed\n\nsetNeedsDisplayInRect:(CGRect)invalidRect\n\n
  • #8: there’s different context: Bitmap, PDF, Printer ...\nContext, Path, Color, Font, Transformation, ...\nUIColor can be easily converted to CGColor\nUIColor is not the best solution. one should cache colors\nCGXXX helper methods\npainter’s model\n
  • #9: coordinate spaces simplify the drawing code required to create complex interfaces\nthe window represents the base coordinate system\ntransforms convert coordinate systems\nCocoa and Quartz coordinate systems are the same\ndifferent coordinate systems UI & CG\ncoordinate space for CG is flipped\nbounds vs frame\n
  • #10: one can create path using CGPath or CGContext. CGContext is “one-time”, while CGPath saves the path.\nGraphic states are implemented as a stack. I can push and pop as many as I need.\n
  • #11: first define the path then draw it!\npath’ can also be used for other operations, e.g. clipping\n
  • #12: \n
  • #13: The important part when using contexts that are created is that I have to release them!!!\nimage context can be used for cached and repeated drawing\n
  • #14: we use “drawTextInRect” because it handles the actual text drawing (color, spacing, ..)\nwe could do it in “drawRect” but then would have to draw the text ourselves\nparams: context, offset size, blur, color\n
  • #15: use translation for flipping coordinate space\nCTM: current transformation matrix\n
  • #16: no Unicode\n
  • #17: linear gradients and radial gradients\ncan be used for glossy effects\n
  • #18: vector based drawing model to scale nicely onto the actual pixels of the device\nuser space vs device space \npoints in the user base might not correspond to pixels on the device!\nyou don’t have to care about the device space unless for adjusting anti-aliasing\nscale factor\n\n
  • #19: vector based drawing model to scale nicely onto the actual pixels of the device\nuser space vs device space \npoints in the user base might not correspond to pixels on the device!\nyou don’t have to care about the device space unless for adjusting anti-aliasing\nscale factor\n\n
  • #20: vector based drawing model to scale nicely onto the actual pixels of the device\nuser space vs device space \npoints in the user base might not correspond to pixels on the device!\nyou don’t have to care about the device space unless for adjusting anti-aliasing\nscale factor\n\n
  • #21: vector based drawing model to scale nicely onto the actual pixels of the device\nuser space vs device space \npoints in the user base might not correspond to pixels on the device!\nyou don’t have to care about the device space unless for adjusting anti-aliasing\nscale factor\n\n
  • #22: vector based drawing model to scale nicely onto the actual pixels of the device\nuser space vs device space \npoints in the user base might not correspond to pixels on the device!\nyou don’t have to care about the device space unless for adjusting anti-aliasing\nscale factor\n\n
  • #23: vector based drawing model to scale nicely onto the actual pixels of the device\nuser space vs device space \npoints in the user base might not correspond to pixels on the device!\nyou don’t have to care about the device space unless for adjusting anti-aliasing\nscale factor\n\n
  • #24: vector based drawing model to scale nicely onto the actual pixels of the device\nuser space vs device space \npoints in the user base might not correspond to pixels on the device!\nyou don’t have to care about the device space unless for adjusting anti-aliasing\nscale factor\n\n
  • #25: vector based drawing model to scale nicely onto the actual pixels of the device\nuser space vs device space \npoints in the user base might not correspond to pixels on the device!\nyou don’t have to care about the device space unless for adjusting anti-aliasing\nscale factor\n\n
  • #26: vector based drawing model to scale nicely onto the actual pixels of the device\nuser space vs device space \npoints in the user base might not correspond to pixels on the device!\nyou don’t have to care about the device space unless for adjusting anti-aliasing\nscale factor\n\n
  • #27: vector based drawing model to scale nicely onto the actual pixels of the device\nuser space vs device space \npoints in the user base might not correspond to pixels on the device!\nyou don’t have to care about the device space unless for adjusting anti-aliasing\nscale factor\n\n
  • #28: \n
  • #29: \n
  • #30: Core Animation was designed for the iPhone\nUIKit was designed with Core Animation in mind\n
  • #31: \n
  • #32: \n
  • #33: \n
  • #34: \n
  • #35: \n
  • #36: Why should we use CALayers instead of UIView Animation\n
  • #37: frame and bounds as in UIView\nchange of position changes the frame\nif you change the anchor point you’ll move the frame!!\nanchorPoint is the position on which all animation occur\n
  • #38: frame and bounds as in UIView\nchange of position changes the frame\nif you change the anchor point you’ll move the frame!!\nanchorPoint is the position on which all animation occur\n
  • #39: frame and bounds as in UIView\nchange of position changes the frame\nif you change the anchor point you’ll move the frame!!\nanchorPoint is the position on which all animation occur\n
  • #40: CALayer\nCAEAGLLayer \n holds Open GL content\nCATiledLayer \n CALayers have a maximum size\n if you exceed this you can use a CATiledLayer\nCAScrollLayer\n layer for scrolling\n probably never use it because of the powerfulness of UIScrollView\nCAReplicatorLayer\n replicates layers\n use it for particles systems\nCAShapeLayer\n holds a Core Graphics path\n lets you animate between path’ (shapes)\n e.g. used for charts\nCAGradientLayer\n hardware accelerated linear gradients\n fastest way to create a gradient\n \nUIView always returns a CALayer class\nif you want a different kind of layer class you have to overwrite\n\n(Class)layerClass\n{\nreturn [CATiledLayer class];\n}\n\n \n
  • #41: presentation tree shows what actually is on screen\nlayer.presentationLayer\n
  • #42: \n
  • #43: \n
  • #44: [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];\nanimation.toValue = (2 * M_PI) * -2\n\n
  • #45: \n
  • #46: \n
  • #47: \n