crafting interactions with core animations, david ortinau

58
Crafting Interactions with Core Animation

Upload: xamarin

Post on 07-May-2015

2.269 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Crafting interactions with Core Animations, David Ortinau

Crafting Interactionswith Core Animation

Page 2: Crafting interactions with Core Animations, David Ortinau

David Ortinau@davidortinauhttp://davidortinau.com

17 yrs web, desktop, interactive, mobile.

BA English, Maryville University

Page 3: Crafting interactions with Core Animations, David Ortinau
Page 4: Crafting interactions with Core Animations, David Ortinau
Page 5: Crafting interactions with Core Animations, David Ortinau
Page 6: Crafting interactions with Core Animations, David Ortinau

Let’s Talk AboutAnimationArchitecture of Core AnimationImplicit Animations and Explicit AnimationsTweening!Real World Use Cases

•••••

Page 7: Crafting interactions with Core Animations, David Ortinau

Animation?

Page 8: Crafting interactions with Core Animations, David Ortinau

http://www.nestmagazine.es/2012/04/entrevista-kyle-bean.html

Page 9: Crafting interactions with Core Animations, David Ortinau
Page 10: Crafting interactions with Core Animations, David Ortinau
Page 11: Crafting interactions with Core Animations, David Ortinau
Page 12: Crafting interactions with Core Animations, David Ortinau
Page 13: Crafting interactions with Core Animations, David Ortinau
Page 14: Crafting interactions with Core Animations, David Ortinau

Interaction Design (IxD) defines the structure andbehavior of interactive systems.

Interaction Designers strive to create meaningfulrelationships between people and the products andservices that they use, from computers to mobiledevices to appliances and beyond.

Page 15: Crafting interactions with Core Animations, David Ortinau

Bill Moggridge’s 3 QuestionsHow do you do?How do you feel?How do you know?

1.2.3.

Page 16: Crafting interactions with Core Animations, David Ortinau

Core Animation Makes it EasyUIView.Animate ( duration: 4, animation: () => { notification.Frame = new RectangleF (newPointF (0, 0), notification.Frame.Size);

} );

Page 17: Crafting interactions with Core Animations, David Ortinau

Architecture

Page 18: Crafting interactions with Core Animations, David Ortinau

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40004514

UIKit /AppKit

OpenGL ES / OpenGL

Graphics Hardware

CoreAnimation

Core Graphics

CAAnimationCAGroupAnimationCAPropertyAnimationCABasicAnimationCAKeyframeAnimationCATransition

CALayeraddSubLayerinsertSubLayersetNeedsDisplaysetNeedsLayout

Page 19: Crafting interactions with Core Animations, David Ortinau

Every UIView has a CALayerUIViews are responsible for interactionsCALayers are responsible for what you see

••

UIView someView = new UIView();someView.LayerLayer.Frame = new RectangleF(0, 0, 100, 100);

Page 20: Crafting interactions with Core Animations, David Ortinau

What you see is a compositing of layersUIViewCALayerUIViewUIViewUIImageUILabelUIButtonUIButtonUIButtonUIButtonUIButtonUIButton

UIView

CALayer

UIView

UIButton

UIButton

UIButton

UIButton

UIButton

UIButton

UIView

UIImage

UILabel

Page 21: Crafting interactions with Core Animations, David Ortinau

CALayerEvery UIView has a layer and sets itself as the delegate for that layerCALayer manages visual content and the geometry of that contentdrawRect creates the layer

•••

https://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html

CALayer someLayer = new CALayer();someLayer.Frame = new RectangleF(0, 0, 300, 40);someLayer.Contents = UIImage.FromFile('path/to/image.png').CGImage;

Page 22: Crafting interactions with Core Animations, David Ortinau

Layer Geometry

https://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html

x

y

+

Bounds

AnchorPoint

Bounds - CGRectPosition - CGPointAnchorPoint - CGPointTransform-CATransform3D

••••

Page 23: Crafting interactions with Core Animations, David Ortinau

What Core Animation ProvidesInterpolationTimingHardware Accelerated. Animations happen on the GPU.Animations are on a background thread

••••

Page 24: Crafting interactions with Core Animations, David Ortinau

CALayer HierarchyCAShapeLayerCATileLayerCATextLayerCAScrollLayerCAReplicatorLayer

•••••

https://developer.apple.com/library/mac/#documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html

Page 25: Crafting interactions with Core Animations, David Ortinau

Animation RenderingLayout is on the CPU. Keep visuals flat for better performance.Animation is on the GPU.

••

WWDC Session 238 iOS App Performance: Graphics and Animations

1. Create animation and update view hierarchy

2. Prepare and commitanimation

3. Render each frame

Page 26: Crafting interactions with Core Animations, David Ortinau

Implicit Animations

Page 27: Crafting interactions with Core Animations, David Ortinau

Implicit Animations of Layer-Backed ViewsUses default timing and animation propertiesUIView must be wrapped in an Animate blockUIView PropertiesFrameCenterBackgroundColorOpacity

•••••••

Page 28: Crafting interactions with Core Animations, David Ortinau

Notifications Demo

Page 29: Crafting interactions with Core Animations, David Ortinau

Animate Block

UIView.Animate ( duration: 4, delay: 0, options: UIViewAnimationOptions.CurveEaseIn, animation: () => { notification.Frame = new RectangleF(new PointF (0, 0), notification.Frame.Size);

} , completion: null);

Page 30: Crafting interactions with Core Animations, David Ortinau

UIViewAnimationOptionsAllowUserInteractionAutoreverseCurveLinear, CurveEaseIn, CurveEaseOut, CurveEaseInEaseOutRepeat

••••

Page 31: Crafting interactions with Core Animations, David Ortinau

UIViewAnimationOptionsEasing is the pace of the animation over time•

Page 32: Crafting interactions with Core Animations, David Ortinau

Glow Pulse Demo

Page 33: Crafting interactions with Core Animations, David Ortinau

UIView.Animate( duration: 1, delay: 0, options: UIViewAnimationOptions.Autoreverse |UIViewAnimationOptions.Repeat,

animation: ()=>{ glow.Alpha = 1; } , completion: null);

Page 34: Crafting interactions with Core Animations, David Ortinau

Explicit Animations

Page 35: Crafting interactions with Core Animations, David Ortinau

Explicit AnimationsCABasicAnimation, CAGroupAnimation, CAKeyframeAnimationMore fine grain control of the animationOnly works on the Layer and doesn’t update the ViewFillMode = CAFillMode.ForwardsRemovedOnCompletion = falseSequence animationsCustom animation of your own properties

•••••••

Page 36: Crafting interactions with Core Animations, David Ortinau

CABasicAnimationKeyPath - the property to animateBeginTime - when in time to start, can be used to staggersequenced animationsDuration - how long the animation should take, scaled to thetimeline of the parentFrom / ToRemoveOnCompletion, FillModeAnimationStopped, AnimationStartedTimingFunction - Linear, EaseIn, EaseOut, EaseInEaseOut

••

••••

Page 37: Crafting interactions with Core Animations, David Ortinau

Attraction Loop Demo

Page 38: Crafting interactions with Core Animations, David Ortinau

FlowcreateSlide() - adds or updates CALayer with new imagestransitionIn() - clears old animations, defines new animations, adds them to

layers, Timer calls transitionOuttransitionOut() - defines out animations, adds to layers, AnimationStopped

signals endcyclesSlides() - increments slide and restarts the loop calling createSlide

Page 39: Crafting interactions with Core Animations, David Ortinau

CABasicAnimationtitleImage.RemoveAllAnimations();

var localMediaTime = CAAnimation.CurrentMediaTime();

var titleAnim = CABasicAnimation.FromKeyPath("position.x");titleAnim.Duration = 1;titleAnim.BeginTime = localMediaTime;titleAnim.From = NSNumber.FromFloat ( 768f );titleAnim.To = NSNumber.FromFloat ( View.Frame.Width * 0.5f );titleAnim.RemovedOnCompletion = false;titleAnim.FillMode = CAFillMode.Forwards;titleAnim.TimingFunction = CAMediaTimingFunction.FromName (CAMediaTimingFunction.EaseOut);

titleImage.AddAnimation ( titleAnim, "position.x" );

timer = new System.Timers.Timer ();timer.Interval = 5000;timer.Elapsed += (object sender, ElapsedEventArgs e) => { timer.Stop(); InvokeOnMainThread(()=>{ transitionOut(); } );} ;timer.Start();

Page 40: Crafting interactions with Core Animations, David Ortinau

Everyone Wants to Spinvar spinAnim = new CABasicAnimation { KeyPath = "transform.rotation.z", To = NSNumber.FromDouble( Math.PI ), Duration = 0.4, Cumulative = true, RepeatCount = 999 } ;

spinner.Layer.AddAnimation( spinAnim,"spinMeRightRoundBabyRightRound" );

Page 41: Crafting interactions with Core Animations, David Ortinau

CAKeyframeAnimationAnimate along a pathSet keyframes for very fine control of the timingPropertiesPath - a bezier curve to followKeyTimes - 1:1 with values, from 0 to 1 over durationValues - the keyframe values at each point

••••••

Page 42: Crafting interactions with Core Animations, David Ortinau
Page 43: Crafting interactions with Core Animations, David Ortinau
Page 44: Crafting interactions with Core Animations, David Ortinau

Infographic Demo

Page 45: Crafting interactions with Core Animations, David Ortinau

PaintCode, DrawScript

Page 46: Crafting interactions with Core Animations, David Ortinau

CAKeyframeAnimationvoid animateDot () {

CAKeyFrameAnimation keyFrameAnimation = (CAKeyFrameAnimation)CAKeyFrameAnimation.FromKeyPath ("position");

keyFrameAnimation.Path = animationPath.CGPath;

keyFrameAnimation.Duration = 10;

keyFrameAnimation.CalculationMode = CAKeyFrameAnimation.AnimationPaced;

keyFrameAnimation.FillMode = CAFillMode.Forwards;

keyFrameAnimation.TimingFunction = CAMediaTimingFunction.FromName

(CAMediaTimingFunction.Linear);

dot.AddAnimation (keyFrameAnimation, "MoveImage");

dot.Position = new PointF (222, 326);

}

Page 47: Crafting interactions with Core Animations, David Ortinau

Easing Tweens

Page 48: Crafting interactions with Core Animations, David Ortinau

Remember This?

Page 49: Crafting interactions with Core Animations, David Ortinau

http://www.robertpenner.com/easing/

Page 50: Crafting interactions with Core Animations, David Ortinau

Bounce Demo

Page 51: Crafting interactions with Core Animations, David Ortinau

Generating Keyframe Values for Easing EquationsNo need to specify KeyTimes as keyframes will be dispersed evenly•

public static float EaseOutBounce(float t, float start, float length) { if ((t) < (1 / 2.75f)) { return length * (7.5625f * t * t) + start; } else if (t < (2 / 2.75)) { return length * (7.5625f * (t -= (1.5f / 2.75f)) * t + .75f) + start; } else if (t < (2.5 / 2.75)) { return length * (7.5625f * (t -= (2.25f / 2.75f)) * t + .9375f) + start; } else { return length * (7.5625f * (t -= (2.625f / 2.75f)) * t + .984375f) + start; } }

http://github.com/debreuil/Swf2XNA, , http://www.robertpenner.com/easing/

Page 52: Crafting interactions with Core Animations, David Ortinau

TweenBuilder

public static NSObject[] CreateKeyValues (float fromValue, float toValue, EasingFormula easingFormula, intsteps = 100)

{ NSObject[] values = new NSObject[steps]; double value = 0; float curTime = 0; for (int t = 0; t < steps; t++) { curTime = (float)t / (float)steps; var easingFactor = easingFormula(curTime, 0, 1); value = (toValue - fromValue) * easingFactor + fromValue;

values[t] = NSNumber.FromDouble(value); } return values;}

Page 53: Crafting interactions with Core Animations, David Ortinau

Using Keyframe Easing Functions

var localMediaTime = CAAnimation.CurrentMediaTime();

NSObject[] keyframes = TweenBuilder.CreateKeyValues(-295, 0, Easing.EaseOutBounce);

var homeIn = new CAKeyFrameAnimation { KeyPath = "position.x", Duration = 1.4, BeginTime = localMediaTime, FillMode = CAFillMode.Forwards, RemovedOnCompletion = false, TimingFunction = CAMediaTimingFunction.FromName(

CAMediaTimingFunction.Linear ), Values = keyframes};

navHome.AddAnimation( homeIn, "homeIn" );

Page 54: Crafting interactions with Core Animations, David Ortinau

If You Can’t Animate It, Fake It

Page 55: Crafting interactions with Core Animations, David Ortinau

Resources

Page 56: Crafting interactions with Core Animations, David Ortinau

ResourcesWWDC 2010

Core Animation In Practicehttps://developer.apple.com/videos/wwdc/2010/?id=424https://developer.apple.com/videos/wwdc/2010/?id=425

WWDC 2011 - Core Animation Essentialshttps://developer.apple.com/videos/wwdc/2011/?id=421

WWDC 2012 - iOS App Performance: Graphics and Animationhttps://developer.apple.com/videos/wwdc/2012/?id=238

Delighting Your Users With Core Animation - Frank Kruegerhttp://www.youtube.com/watch?v=6JePwHjVj6U&noredirect=1http://www.slideshare.net/Xamarin/delight-your-users-with-coreanimation

About Core Animationhttps://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreAnimation_guide/Introduction/Introduction.html

Robert Penners Easing Functionshttp://www.robertpenner.com/easing/

Robin Debreuil’s Swf2XNA Respositoryhttp://github.com/debreuil/Swf2XNA

Page 57: Crafting interactions with Core Animations, David Ortinau

If We Have TimeHow to interact with UIView during animation. Notification demo.How to kill an animation. Attraction Loop demo.

••

Page 58: Crafting interactions with Core Animations, David Ortinau

Thanks!

@davidortinauhttp://davidortinau.com [email protected]