section ios 4
TRANSCRIPT
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 1/39
iOS: Core
Graphics andCore
Animation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
iOS: Core Graphics and Core Animation
Tommy MacWilliam
Harvard University
April 19, 2011
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 2/39
iOS: Core
Graphics andCore
Animation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Announcements
Lectures: http://cs76.net/Lectures
Sections: http://cs76.net/Sections
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 3/39
iOS: Core
Graphics andCore
Animation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Today
Core Graphics Drawing
Working with Images
Core Animation
Integrating with Other Apps
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 4/39
iOS: Core
Graphics andCore
Animation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Quartz
2D drawing engine for iOS
works with all Core Animation, OpenGL ES, and UIKit
layers painted to canvas
objects drawn in the order the appear
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 5/39
iOS: Core
Graphics andCore
Animation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Context
CGContextRef: where to draw output
UIGraphicsGetCurrentContext() for currentwindow
also PDFs, bitmaps, etc.
drawRect: UIView method called to draw contents
setNeedsDisplay to force redraw
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 6/39
iOS: Core
Graphics andCore
Animation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Paths
defined by CGMutablePathRef (created via
CGPathCreateMutable())
point: single point in 2D space
CGPathMoveToPoint
line: defined by endpoints (one endpoint is current
point, so define other)
CGPathAddLineToPoint
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 7/39
iOS: Core
Graphics andCore
Animation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Paths
cubic Bezier curves: defined by 3 control points and
endpoint CGPathAddCurveToPoint
quadratic Bezier curves: defined by 2 control points
and endpoint
CGPathAddQuadCurveToPoint
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 8/39
iOS: Core
Graphics andCore
Animation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Paths
CGPathCloseSubpath: close path
CGContextAddPath: add path to context
CGContextFillPath: create shape from path
CGContextStrokePath: create outline from path
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 9/39
iOS: Core
Graphics andCoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Paths
CGContextSetLineWidth: set stroke weight
CGContextSetLineCap: set how lines end
CGContextSetLineDash: draw dotted line
CGContextSetStrokeColorWithColor : set color
(UIColors have property for CGColor)
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 10/39
iOS: Core
Graphics andCoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Paths
example time!
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 11/39
iOS: Core
Graphics andCoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Shadows
need x offset, y offset, blur CGContextSetShadow or
CGContextSetShadowWithColor to draw shadows
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 12/39
iOS: Core
Graphics andCoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Saving State
push and pop from stack of states
resetting everything is annoying
CGContextSaveGState: push state onto stack
CGContextRestoreGState: pop value off stack
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 13/39
iOS: Core
Graphics andCoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Shadows and State
example time!
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 14/39
iOS: Core
Graphics andCoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Gradients
CGGradientRef can create axial and radial gradients
(CGGradientCreateWithColorComponents )
axial: color varies along line (same color alongperpendicular)
radial: color varies along concentric circles (same coloralong given circumference)
need color space, colors, and locations for each color
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 15/39
iOS: Core
Graphics andCoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Color Spaces
CGColorSpaceCreateDeviceRGB(): RGB (red,
green, blue) CGColorSpaceCreateDeviceCMYK(): CMYK (cyan,
magenta, yellow, key)
CGColorSpaceCreateDeviceGray(): grayscale
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 16/39
iOS: Core
Graphics andCoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Gradients
example time!
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 17/39
iOS: Core
Graphics andCoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Custom Views
drawRect is a method of any UIView subclass UIView, UIButton, etc. to create custom
views
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 18/39
iOS: Core
Graphics andCoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Custom Views
example time!
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 19/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
UIImageView
image: UIImage to be displayed imageWithData can load remote URL
handles scaling and aspect ratios for you!
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 20/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
UIImagePickerController
modal view controller allowing for selection of image
from library sourceType: where to get images from
presentModalViewController (just like any other
view controller) to display
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 21/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
UIImagePickerControllerDelegate
make sure to set delegate property!
imagePickerController:didFinish
PickingMediaWithInfo: user selected image can get UIImagePickerControllerEditedImage
or UIImagePickerControllerOriginalImage
from passed dictionary
imagePickerControllerDidCancel: user clicked“Cancel” instead of selecting image
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 22/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Image Picker
example time!
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 23/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
UIImage
UIImage ready to use with Core Graphics via
CGImage
drawInRect: draw UIImage in given CGRect
CGContextDrawImage: draw CGImage in given
CGRect
different coordinate system, so image will be
upside-down!
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 24/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Transforms
CGContextRotateCTM: rotate about a point CGContextScaleCTM: change size
CGContextTranslateCTM: move in a direction
Cli i
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 25/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Clipping
rectangles are boring! remember CGMutablePathRef?
CGContextClip restricts all drawings to last path
UII
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 26/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
UIImage
example time!
C A i ti
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 27/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Core Animation
animate 2D layers in 3D space implicit: set new properties, get smooth animation
explicit: full control over timing, etc.
C A i ti
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 28/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Core Animation
add QuartzCore.framework to project
#import “QuartzCore/CAAnimation.h”
Implicit Animation
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 29/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Implicit Animation
static methods to UIViews set animation properties
enclosed in beginAnimations:context: and
commitAnimation
changed properties will animate automatically
Implicit Animation
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 30/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Implicit Animation
setAnimationTransition: lots of stock animations
built in
setAnimationDuration: time, in seconds, of
animation
setAnimationDelegate,
setAnimationDidStopSelector: register callbacks
Implicit Animation
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 31/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Implicit Animation
example time!
Layers
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 32/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Layers
CALayer has contents to be animated
UIView has underlying layer, so we can animatethem
provide content via content property, via delegate, or
subclass
Keyframe Animation
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 33/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Keyframe Animation
CAKeyframeAnimation creates a custom animation
key path specifies what property will be animated animatable properties: http://developer.apple.
com/library/ios/#documentation/Cocoa/
Conceptual/CoreAnimation_guide/Articles/
AnimProps.html
Animation Paths
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 34/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Animation Paths
CGMutablePathRef again!
path specifies path layer can be animated along
(position keypath)
duration for animation in seconds
Transforms
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 35/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Transforms
CATransform3DMakeRotation: rotation matrix
CATransform3DMakeScale: scaling matrix CATransform3DMakeTranslation: translation
matrix
values gives NSArray of frames
Keyframe Animations
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 36/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Keyframe Animations
example time!
Opening Other Apps
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 37/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Opening Other Apps
just like Android, other apps opened via URLs
[[UIApplication sharedApplication]openURL:url]
where url is an NSURL (schemes like http://,
tel:, sms:)
Being Opened by Other Apps
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 38/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Being Opened by Other Apps
specify your own URLs with <app
name>-Info.plist (like AndroidManifest.xml)
application:handleOpenURL: fired when app
opened from URL
Integrating with Other Apps
8/2/2019 Section Ios 4
http://slidepdf.com/reader/full/section-ios-4 39/39
iOS: CoreGraphics and
CoreAnimation
TommyMacWilliam
Core GraphicsDrawing
Working withImages
CoreAnimation
Integratingwith OtherApps
Integrating with Other Apps
example time!