section ios 4

39
iOS: Core Graphics and Core Animation Tommy MacWilliam Core Graphics Drawing Working with Images Core Animation Integrating with Other Apps iOS: Core Graphics and Core Animation Tommy MacWilliam Harvard University April 19, 2011

Upload: onlinestuffm7437

Post on 06-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Section Ios 4

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

Page 2: Section Ios 4

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

Page 3: Section Ios 4

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

Page 4: Section Ios 4

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

Page 5: Section Ios 4

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

Page 6: Section Ios 4

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

Page 7: Section Ios 4

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

Page 8: Section Ios 4

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

Page 9: Section Ios 4

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)

Page 10: Section Ios 4

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!

Page 11: Section Ios 4

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

Page 12: Section Ios 4

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

Page 13: Section Ios 4

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!

Page 14: Section Ios 4

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

Page 15: Section Ios 4

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

Page 16: Section Ios 4

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!

Page 17: Section Ios 4

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

Page 18: Section Ios 4

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!

Page 19: Section Ios 4

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!

Page 20: Section Ios 4

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

Page 21: Section Ios 4

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

Page 22: Section Ios 4

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!

Page 23: Section Ios 4

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!

Page 24: Section Ios 4

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

Page 25: Section Ios 4

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

Page 26: Section Ios 4

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

Page 27: Section Ios 4

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

Page 28: Section Ios 4

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

Page 29: Section Ios 4

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

Page 30: Section Ios 4

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

Page 31: Section Ios 4

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

Page 32: Section Ios 4

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

Page 34: Section Ios 4

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

Page 35: Section Ios 4

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

Page 36: Section Ios 4

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

Page 37: Section Ios 4

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

Page 38: Section Ios 4

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

Page 39: Section Ios 4

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!