Upload
others
View
5
Download
0
Embed Size (px)
Citation preview
CORE ANIMATION: HANDS-ONBuilding Complex and Attractive Cocoa Touch Interfaces
© 2009 Nathan Eror & Free Time Studios
Nathan Eror - Free Time Studios
WHO AM I?• Nathan Eror - Free Time Studios
• 10 years as a developer (8 of those on a Mac ☻)
• Game console tools
• Java 2D graphics library
• Web and Hosting
• iPhone SDK since the beginning
© 2009 Nathan Eror & Free Time Studios
WTF?• Core Animation
• Lots of detail
• A lot of code (>1400 lines of it!)
• Get the code at github: http://github.com/neror/CA360
• Here we go!
© 2009 Nathan Eror & Free Time Studios
WHAT IS CORE ANIMATION?(Surprise! It’s Not Only About Animation.)
• Compositing and animation API created for Cocoa Touch
• Hierarchical collection of layers
• Highly optimized for speed (only draws when necessary)
• Interpolates animations away from the main thread
• Simple API (most of the time)
• Hardware accelerated for high performance
© 2009 Nathan Eror & Free Time Studios
CORE ANIMATION & UIKIT• Core Animation is at the heart of UIKit
• It was originally designed for Cocoa Touch and ported to Leopard
• A UIView is essentially a CALayer with touch event handling
• A UIView is composed of a single CALayer and is the layer’s drawing delegate (more about that later)
© 2009 Nathan Eror & Free Time Studios
UIView.hUIKIT_EXTERN_CLASS @interface UIView : UIResponder<NSCoding> { @package CALayer *_layer;}
// default is [CALayer class]. Used when creating the underlying layer for the view.+ (Class)layerClass;
// returns view's layer. Will always return a non-nil value. view is layer's delegate@property(nonatomic,readonly,retain) CALayer *layer;
CORE ANIMATION IPHONECLASS HIERARCHY
© 2009 Nathan Eror & Free Time Studios
LAYER GEOMETRY&
TRANSFORMS
© 2009 Nathan Eror & Free Time Studios
GEOMETRY & TRANSFORMS• 2D cartesian coordinate system with origin in the top left
• Quartz2D primitive data structures (CGPoint, CGRect, etc.)
• Each layer has its own coordinate system
• All geometric properties are animatable and have a default implicit animation
• A CALayer is a 2D plane projected in 3D
• The transition to the next slide is a good illustration of this
© 2009 Nathan Eror & Free Time Studios
X
Y
GEOMETRY & TRANSFORMS
CALayer Geometric Properties@property CGRect bounds;
@property CGPoint position;
@property CGFloat zPosition;
@property CGPoint anchorPoint;
@property CGFloat anchorPointZ;
@property CATransform3D transform;
@property CGRect frame;
© 2009 Nathan Eror & Free Time Studios
Quartz2D Data Structuresstruct CGPoint { CGFloat x; CGFloat y;};typedef struct CGPoint CGPoint;
struct CGSize { CGFloat width; CGFloat height;};typedef struct CGSize CGSize;
struct CGRect { CGPoint origin; CGSize size;};typedef struct CGRect CGRect;
3D Transform Properties
Parent Layer (L1)
L1.bounds.origin = (0,0)
L1.bounds.width = 500
L1.bounds.height = 400
GEOMETRY & TRANSFORMS
L1.transform = CATransform3DIdentity
Child Layer (L2)
L2.bounds.height = 150
L2.bounds.width = 150
L2.bounds.origin = (0,0)
L2.anchorPoint = (0.5,0.5)
L2.frame.origin = (280,280)
L2.frame.width = 150
L2.frame.height = 150
L2.position = (355,355)
L2.transform = CATransform3DIdentity
L1.sublayerTransform = CATransform3DIdentity
© 2009 Nathan Eror & Free Time Studios
X
Y
GEOMETRY & TRANSFORMS
© 2009 Nathan Eror & Free Time Studios
CODE DEMOGeometric Properties
GEOMETRY & TRANSFORMS3D Transformation Matrix Data Structurestruct CATransform3D{ CGFloat m11, m12, m13, m14; CGFloat m21, m22, m23, m24; CGFloat m31, m32, m33, m34; CGFloat m41, m42, m43, m44;};typedef struct CATransform3D CATransform3D;
CALayer’s 3D Transformation Properties@property CATransform3D transform;
@property CATransform3D sublayerTransform;
@property CGPoint anchorPoint;
@property CGFloat anchorPointZ;
© 2009 Nathan Eror & Free Time Studios
CATransform3D Matrix OperationsCATransform3D CATransform3DMakeTranslation(CGFloat tx, CGFloat ty, CGFloat tz);
CATransform3D CATransform3DMakeScale(CGFloat sx, CGFloat sy, CGFloat sz);
CATransform3D CATransform3DMakeRotation(CGFloat angle, CGFloat x, CGFloat y, CGFloat z);
CATransform3D CATransform3DTranslate(CATransform3D t, CGFloat tx, CGFloat ty, CGFloat tz);
CATransform3D CATransform3DScale(CATransform3D t, CGFloat sx, CGFloat sy, CGFloat sz);
CATransform3D CATransform3DRotate(CATransform3D t,CGFloat angle,CGFloat x,CGFloat y,CGFloat z);
CATransform3D CATransform3DConcat(CATransform3D a, CATransform3D b);
CATransform3D CATransform3DInvert(CATransform3D t);
GEOMETRY & TRANSFORMS
© 2009 Nathan Eror & Free Time Studios
CODE DEMOLayer Transforms
CATransform3Drotation rotation.x rotation.y rotation.z
scale scale.x scale.y scale.z
translation translation.x translation.y translation.z
© 2009 Nathan Eror & Free Time Studios
KEY VALUE CODINGAll CALayer properties are available through key paths//Scale the layer along the x-axis[layer setValue:[NSNumber numberWithFloat:1.5] forKeyPath:@"transform.scale.x"];
//Same result as aboveCGSize biggerX = CGSizeMake(1.5, 1.);[layer setValue:[NSValue valueWithCGSize:biggerX] forKeyPath:@"transform.scale"];
CGPointorigin size
origin.x size.width
origin.y size.height
CGSize
width
height
CGPoint
x
y
! Structures must be wrapped in an NSValue instance before being passed to setValue:forKeyPath:
Key Paths for Structure Fields:
LAYER HIERARCHY
© 2009 Nathan Eror & Free Time Studios
© 2009 Nathan Eror & Free Time Studios
Layer Tree Hierarchy Related Methods
LAYER TREE HIERARCHY• Layers are organized in a tree
• A layer is positioned, sized and transformed relative to its parent’s coordinate system and sublayerTransform
• By default, a parent layer does not clip its children to its bounds
@property(readonly) CALayer *superlayer; - (void)removeFromSuperlayer;@property(copy) NSArray *sublayers;
- (void)addSublayer:(CALayer *)layer;- (void)insertSublayer:(CALayer *)layer atIndex:(unsigned)idx;- (void)insertSublayer:(CALayer *)layer below:(CALayer *)sibling;- (void)insertSublayer:(CALayer *)layer above:(CALayer *)sibling;- (void)replaceSublayer:(CALayer *)layer with:(CALayer *)layer2;
@property CATransform3D sublayerTransform;
© 2009 Nathan Eror & Free Time Studios
CODE DEMOLayer Tree
LAYER TREE HIERARCHY
LAYER CONTENT & STYLE
© 2009 Nathan Eror & Free Time Studios
LAYER CONTENT & STYLE• Manipulate a layer’s appearance through the style properties
or custom drawing
• Custom drawing is accomplished one of three ways (from easiest to hardest):
• Set the contents property to a CGImageRef
• Set a delegate and implement the drawing method(s)
• Subclass CALayer
© 2009 Nathan Eror & Free Time Studios
LAYER CONTENT & STYLE
© 2009 Nathan Eror & Free Time Studios
Content Properties@property(retain) id contents;
@property CGRect contentsRect;
@property(copy) NSString *contentsGravity;
@property CGRect contentsCenter;
@property(getter=isOpaque) BOOL opaque;
@property BOOL needsDisplayOnBoundsChange;
Style Properties@property CGColorRef backgroundColor;
@property CGFloat cornerRadius;
@property CGFloat borderWidth;
@property CGColorRef borderColor;
@property float opacity;
@property(retain) CALayer *mask;
Content Methods- (void)display;
- (void)setNeedsDisplay;
- (void)setNeedsDisplayInRect:(CGRect)r;
- (BOOL)needsDisplay;
- (void)displayIfNeeded;
- (void)drawInContext:(CGContextRef)ctx;
Drawing Delegate Methods- (void)displayLayer:(CALayer *)layer;
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx;
© 2009 Nathan Eror & Free Time Studios
CODE DEMOSImage Content
Delegate DrawingStyle Properties
LAYER CONTENT & STYLE
ANIMATION(FINALLY!)
© 2009 Nathan Eror & Free Time Studios
ANIMATION IN ENGLISH
© 2009 Nathan Eror & Free Time Studios
Move the box from the left position to the right position over 2 seconds distributing the interpolated values along a curve that slopes slowly at the beginning, quickly through the middle and slowly again at the end.
ANIMATION OVERVIEW• An animation is defined by the property it is animating, the time it
takes, and the pacing of the interpolation
• All animatable properties have a default implicit animation
• All animatable properties can be explicitly animated as well
• Animations do not effect underlying geometric and style properties, only the rendering (presentation) of the layer
• All animation classes descend from CAAnimation
• A CAAnimation notifies an optional delegate at the start and end of its animation
© 2009 Nathan Eror & Free Time Studios
ANIMATION TIMING• The CAMediaTiming protocol is responsible for the duration
and repeating of animations
• An animation’s timing is defined relative to its parent layer’s timespace (controlled by the speed property)
• The fillMode property defines the animation’s behavior outside of its active duration
• The CAMediaTimingFunction describes the pacing of the animation as a cubic bezier curve
© 2009 Nathan Eror & Free Time Studios
ANIMATION TIMING
© 2009 Nathan Eror & Free Time Studios
Default Timing Functions
• Use CABasicAnimation to explicitly animate a layer property
• toValue: The final value of the property
• fromValue: The optional starting value
• byValue: Add byValue to the current value
© 2009 Nathan Eror & Free Time Studios
One PropertyCABasicAnimation
CAKeyframeAnimation
n PropertiesCAAnimationGroup
Layer Tree ActionsCATransition
PROPERTY ANIMATION
© 2009 Nathan Eror & Free Time Studios
CODE DEMOBasic Animation
PROPERTY ANIMATION
• Use CAAnimationGroup to group multiple property animations to be run concurrently on the same layer
• The delegate and removedOnCompletion properties of the animations are ignored
• The duration, fillMode and beginTime properties of the property animations offer fine grained control over each animation
• The durations of the animations are clipped to the duration of the group, not scaled.
© 2009 Nathan Eror & Free Time Studios
One PropertyCABasicAnimation
CAKeyframeAnimation
n PropertiesCAAnimationGroup
Layer Tree ActionsCATransition
ANIMATION GROUPS
© 2009 Nathan Eror & Free Time Studios
CODE DEMOAnimation Groups
ANIMATION GROUPS
• CAKeyframeAnimation allows more fine control over the animation of a layer property
• values: Array of toValues
• path: Animate along a CGPathRef (alt. to values)
• calculationMode: Interpolation style between keyframes
© 2009 Nathan Eror & Free Time Studios
One PropertyCABasicAnimation
CAKeyframeAnimation
n PropertiesCAAnimationGroup
Layer Tree ActionsCATransition
KEYFRAME ANIMATION
© 2009 Nathan Eror & Free Time Studios
CODE DEMOKeyframe Animation
KEYFRAME ANIMATION
TRANSITIONS• A CATransition animates changes to the
layer tree
• Canned animations that effect an entire layer
• The type and subtype properties define the canned transition animation
• The CATransition is added to the parent layer and animates the hiding, showing, adding and removing of its child layers
© 2009 Nathan Eror & Free Time Studios
One PropertyCABasicAnimation
CAKeyframeAnimation
n PropertiesCAAnimationGroup
Layer Tree ActionsCATransition
© 2009 Nathan Eror & Free Time Studios
CODE DEMOLayer Transitions
TRANSITIONS
ACTIONS & TRANSACTIONS
© 2009 Nathan Eror & Free Time Studios
• A CATransaction groups changes to the layer tree into a single atomic update to the display
• Useful for temporarily disabling default layer actions
• Each step of the runloop opens and closes a CATransaction
• Layer actions are animations that are triggered by a change to an animatable layer property or the layer tree
• The implicit property animations are implemented as layer actions
CALayerDelegate Method to Select an Action- (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event;
© 2009 Nathan Eror & Free Time Studios
CODE DEMOSAnimation Transactions
Layer Actions
ACTIONS & TRANSACTIONS
SPECIAL LAYER TYPES
© 2009 Nathan Eror & Free Time Studios
SPECIAL LAYER TYPES• CAScrollLayer: CALayer subclass for scrollable content
• CATiledLayer: For displaying very large layers (bigger than 1024x1024) at multiple levels of detail
• CAShapeLayer: Draws an animatable CGPath in its coordinate space (new in 3.0)
• CAGradientLayer: Draws a color gradient over its background color (new in 3.0)
• CATransformLayer: A container layer used to create a true 3D layer hierarchy (new in 3.0)
• CAReplicatorLayer: Creates copies of its sublayers with each copy potentially having geometric, temporal and color transformations applied to it
© 2009 Nathan Eror & Free Time Studios
© 2009 Nathan Eror & Free Time Studios
CODE DEMOSSpecial Layer Types
Miscellaneous Tips & Tricks
SPECIAL LAYER TYPES
© 2009 Nathan Eror & Free Time Studios
THANK YOUNathan Eror
http://twitter.com/freetimestudios
http://www.freetimestudios.com
http://twitter.com/neror
http://www.neror.com