core animation

33
http://bobmccune.com Core Animation Creating Animated UIs with

Upload: bob-mccune

Post on 15-Jan-2015

16.008 views

Category:

Technology


2 download

DESCRIPTION

Core Animation prepared for March 2011 Minnesota CocoaHeads meeting. Sample code available at: https://github.com/tapharmonic/Core-Animation-Demos

TRANSCRIPT

Page 1: Core Animation

http://bobmccune.com

Core AnimationCreating Animated UIs with

Page 2: Core Animation

Agenda

• Overview of Core Animation• Layers

• Animations

• Transformations

• Practical Animations• UIKit Animations

• New features in iOS 4

Page 3: Core Animation

Why is animation important?

Page 4: Core Animation

Why is animation important?• Provides Context

• Aides in Learning

• Natural Interface

• Consistency

Page 5: Core Animation

Core Animation

• Lightweight, high-performance compositing• Hardware accelerated

• Animations run on separate threads

• Simple, familiar programming model

• Powerful, yet easy to use

The “Magic” in iOS

Page 6: Core Animation

Core Animation Architecture

UIKit

Core Animation

Open GL

Hardware

Renderedin GPU

Page 7: Core Animation

Understanding Layers

Page 8: Core Animation

Core Animation Layers

• Foundational component of Core Animation• Found in Quartz Core

• Programming model similar to views• addSublayer• layoutSublayers• insertSublayer:atIndex:• 2 1/2 D Model• Transform 2D plane in 3D space

•Works with Core Graphics types

Page 9: Core Animation

CALayer Content

• Set contents property• CGImageRef• Draw content in CGContextRef• Delegation: drawLayer:inContext:• Subclass: drawInContext:• Specialized Layers:• CAShapeLayer, CATextLayer, AVPlayerLayer, CAGradientLayer, etc.

Page 10: Core Animation

CALayer ExampleUIImage *image = [UIImage imageNamed:@"ca_logo.png"];

CALayer *logoLayer = [CALayer layer];logoLayer.bounds = CGRectMake(0, 0, image.size.width, image.size.height);logoLayer.borderColor = [UIColor whiteColor].CGColor;logoLayer.border = 1.0;logoLayer.position = CGPointMake(160, 180);logoLayer.contents = (id)image.CGImage;

Page 11: Core Animation

Layer Geometry

• bounds• position• transform• anchorPoint• frame

anchorPoint

bounds

X

Y

position

Page 12: Core Animation

Creating Animations

Page 13: Core Animation

Implicit Animations

• Almost all CALayer properties are animatable

• Property changes trigger implicit animations• Animation duration over 1/4 second

• Uses linear animation curve by default

• Multiple changes batched into transaction

megaManLayer.position = CGPointMake(200, 100);

Page 14: Core Animation

Explicit Animations

• Core Animation provides support for explicit animations• Provides more fine-grained control

• Primary classes• CABasicAnimation• CAKeyframeAnimation• CAAnimationGroup

Page 15: Core Animation

Basic Animations

• Create an animation for a keyPath:• @"position"• @"transform.rotation.z"• @"backgroundColor"• Set its toValue and fromValue properties

• Add the animation to the layer to animate

CABasicAnimation

Page 16: Core Animation

CABasicAnimationExample

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];animation.fromValue = [NSNumber numberWithFloat:1.0];animation.toValue = [NSNumber numberWithFloat:0.0];animation.duration = 1.0;[layer addAnimation:animation forKey:@"animateOpacity"];

Page 17: Core Animation

CABasicAnimationExample

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];animation.fromValue = [NSNumber numberWithFloat:1.0];animation.toValue = [NSNumber numberWithFloat:0.0];animation.duration = 1.0;layer.opacity = 0.0;[layer addAnimation:animation forKey:@"animateOpacity"];

Gotcha Alert

Update the model to make it stick

Page 18: Core Animation

Key Frame Animations

• Provides more advanced animation behavior

• Animate across collection of "key frames"• path (CGPathRef)

• values (NSArray of values)

• Can define timing of each point or value

CAKeyframeAnimation

Page 19: Core Animation

CAKeyframeAnimation

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];animation.path = path; // CGPathRefanimation.duration = 2.0f;animation.rotationMode = kCAAnimationRotateAuto;[layer addAnimation:animation forKey:@"animatePosition"];

Example

Page 20: Core Animation

Transforming Layers

• Layer transformations with CATransform3D• Similar to CGAffineTransform• Transforms can be performed in X, Y, and Z

• Transformations created using:• CATransform3DScale()• CATransform3DRotate()• CATransform3DTranslate()• Can set transform and sublayerTransform

CATransform3D

Page 21: Core Animation

CATransform3D

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

// Animate 2, counter-clockwise 360 degree rotationsCGFloat radians = (2 * M_PI) * -2animation.toValue = [NSNumber numberWithFloat:radians];animation.duration = 0.75f;

[layer addAnimation:animation forKey:@"animateRotation"];

Example

Page 22: Core Animation

Animation Demos

Page 23: Core Animation

UIKit Animations

Page 24: Core Animation

UIView Animations• UIView is a lightweight wrapper over CALayer• Every view has an associated CALayer• Override layerClass class method to change

instance

• UIKit provides support for common animations• Behavior for free with animated:YES• Low cost view transitions

• Uses Core Animation under the hood

Page 25: Core Animation

Core Animation and UIKit

• View and Layers are complimentary

• CA layers provide no event handling• Manual hit testing

• No gesture recognizers

• Layers provides more animatable properties

• Layers provide additional capabilities• cornerRadius, border, shadows

• Need Core Animation to move beyond stock animations

Page 26: Core Animation

Animating Views in iOS < 4.0

[UIView beginAnimations:@"fadeMe" context:NULL];[UIView setAnimationDuration:1.0];self.megaManView.alpha = 0.0f;[UIView commitAnimations];

Page 27: Core Animation

View Animations in iOS 4

[UIView animateWithDuration:1.0 animations:^{ self.megaManView.alpha = 0.0f;}];

Page 28: Core Animation

Blocks-based Animation API

animateWithDuration: animations:

animateWithDuration: animations: completion:

animateWithDuration: delay: options: animations: completion:

Simple Animations

Simple Animations withcompletion callback

More complex with customizable options

Page 29: Core Animation

Blocks-based Animation API

animateWithDuration: animations:

animateWithDuration: animations: completion:

animateWithDuration: delay: animations: completion:

Simple Animations

Simple Animations withcompletion callback

More complex with customizable options

options:

Page 30: Core Animation

Animation Options

• Bit mask to specify animation behavior• Repeating and autoreverse behavior

• Enabling user interaction• Blocks-based API defaults to NO

• Specifying animation curves

• Beginning an animation from current state

• Property inheritance

UIViewAnimationOption

Page 31: Core Animation

View Transitions

• UIView provides common animated transitions• Reveals, Slides, Flips, Curls, etc.

• Transitions performed using:• transitionWithView:• transitionFromView:toView:• Transitions behavior customized with

animation options mask

Page 32: Core Animation

Animation Demos

Page 33: Core Animation

Summary

• CA provides powerful, easy to use API• Lightweight

• Hardware accelerated

• Robust animations without OpenGL

• UIView and Core Animation• UIKit provides support for common

animations

• Can use Core Animation directly where more power and flexibility is needed