Animation in iOS


Published on

This presentation reviews the various approaches you can take to creating animations in your iOS apps.

It discusses UIKit animations, including curling pages up (as in the Maps app), flipping back and forth (as in the Stocks app) and image view animations.
It also describes Core Animation, including its implicit animations that occur when you change the value of certain animatable properties, as well as how to create explicit animations.

1 Comment
  • 10---Bom dia amigos, Venham conhecer nossos Servidores de Mu Online
    Online Season 6 O n
    Fazemos sua Diversão com qualidade,há mais de 5 anos
    Servers ON 24 horas por dia.
    Vários Server esperando por você.Venha se divertir de verdade.
    Venham participar do 6° Megaultrasuperhiper Evento Castle Siege
    Sejam benvindos ao nosso Servidor.
    >>Venhas conhecer o mais novo Servidor o PHOENIX acumulativo,Max Stats 32767.
    >>Kits DIAMOND,Com asas LVL 4,os kits mais fortes e raros do servidor ,venham conferir.
    >>Qual o seu Time ?? Tenha o seu Time do coração no Mu Online.Sets do seu Time preferido
    >>Confiram a nova versão do Shild mais usado no servidor o SHILD POWER v3.
    >> Lançamento do NOVO Supremo Kit Fusion V2 + Kit Mystical Supremo,Kit hiper top,forte e bonito.
    Você só encontra no
    Site HYCLEN Divulgadora Oficial !!!
    Tenham todos um bom Domingo !!!
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • - Most of the topics I cover today have a dedicated file in the sample code demonstrating the techniques and methods I'll be talking about\n
  • \n
  • \n
  • \n
  • \n
  • - Who is already using blocks in their code?\n- Who is not using blocks just yet, or isn't familiar with what blocks are?\n
  • - Would any of you in the first group like to explain blocks in 30 seconds?\n
  • \n
  • - How many of you are using blocks now in your code?\n- Who would like to take a shot at explaining what blocks do?\n
  • - How many of you are using blocks now in your code?\n- Who would like to take a shot at explaining what blocks do?\n
  • - How many of you are using blocks now in your code?\n- Who would like to take a shot at explaining what blocks do?\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • - I always\n
  • - I always\n
  • - I always forget to do this, and then waste time puzzling over compile errors \n
  • - Since Core Animation is all about using CALayers, first we need to add some content to them!\n
  • - I'm not going to go through this, but it's covered thoroughly in the docs\n- For the rest of the talk, let's just assume your CALayers already have some content in them, and you want to animate those layers\n
  • \n
  • \n
  • - this is a bit about how it works under the covers\n
  • \n
  • \n
  • - how do you tell CA what property you are animating? you use a keyPath\n- you explicitly add the animation to the layer -- that’s how you kick off the animation\n
  • \n
  • - you have NOT changed your layer model\n- if you query the model during the animation, you’ll get the old value, not the presentation you’re seeing.\n
  • - you have NOT changed your layer model\n- if you query the model during the animation, you’ll get the old value, not the presentation you’re seeing.\n
  • \n
  • \n
  • \n
  • \n
  • Animation in iOS

    1. 1. Animation in iOS Alexis Goldstein @alexisgoldstein
    2. 2. Agenda for Today• Animation Sample Code• UIView Animations: • Animatable Properties • Animation Options • Transitions• CoreAnimation: • Implicit Animations • Explicit Animations• Where to go to learn more
    3. 3. Sample Code• I’ve created some basic sample code to go along with this talk. • I leveraged Nathan Eror’s CA360 project as a base, and then added all new examples for this talk.• You can find my sample code at: http://• You can find these slides at: alexisAnim
    4. 4. About Me• Spent seven years in technology on Wall Street.• Quit to pursue my dreams of going indie.• Launched aut faciam, an iOS software house, in July 2010.• Director of Operations for Girl Develop IT: • Aim is to provide low-cost, non-intimidating programming classes for women.
    5. 5. About You• How long have you been working with iOS?• What do you want to animate / what kinds of things have you used animation for?
    6. 6. UIView Animations
    7. 7. UIView Animations• Powered by CoreAnimation.• Hardware Accelerated: • Animations happen on the GPU, rather than the CPU, to improve performance.
    8. 8. UIView Animations• How you perform a UIView animation depends on the iOS version.• iOS 4.0 and later: • block-based animation methods, such as animateWithDuration: animations: completion: • iOS 3.2 and earlier: • beginAnimation and commitAnimations
    9. 9. Blocks (in 30 seconds)• Blocks are objects that encapsulate a section of code that can be executed at any time.• ^ introduces a block.• When used as method or function arguments, blocks are a type of callback. • “When invoked, the method or function performs some work and, at the appropriate moments, calls back to the invoking code—via the block —to request additional information or to obtain application-specific behavior from it.” -- A Short Practical Guide to Blocks• Blocks have access to the local variables, parameters & even stack variables of the method you define it in.• Blocks also have access to functions and global variables, including instance variables.
    10. 10. UIView Animations: what we’ll focuson today • How you perform a UIView animation depends on the OS version. • iOS 4.0 and later: • block-based animation methods, such as animateWithDuration: animations: completion: • iOS 3.2 and earlier: • beginAnimation and commitAnimations
    11. 11. UIView Animations• There are certain properties in UIView that are animatable.• You create UIView animations by changing the value of these animatable properties from within an animation block (iOS 4.0+) animations:^ { ... }
    12. 12. UIView Animatable PropertiesProperty Description frame The view’s frame rectangle bounds The view’s bounding rectangle center The center of the frame The transform applied to the view,transform relative to the center of its bounds alpha The view’s level of transparency
    13. 13. UIView’s animateWithDuration• The most basic method to use to create UIView animations is animateWithDuration: animations:
    14. 14. Animating a change in Alpha[UIView animateWithDuration:1.0 animations:^ { [myView setAlpha:1.0]; }];Demo: SetAlpha.m in the sample code
    15. 15. Animating a change in Center[UIView animateWithDuration:1.0 animations:^ { [myView setCenter: CGPointMake(200, 200)]; }];
    16. 16. Animating a Transform [UIView animateWithDuration:1.0 animations:^ { [myView setTransform: CGAffineTransformMakeRotatio n(M_PI)]; }];
    17. 17. Nesting Animation Blocks• You can nest animations inside one another• See NestedAnimations.m in the sample code • AnimationTalk
    18. 18. UIView Animation Options• You can also configure the animation through UIViewAnimationOptions.• In iOS 4.0+, these are set through the options: argument of animateWithDuration: delay: options: animations: completion: • In iOS 3.2 and earlier: Setter methods in several class methods of UIView.
    19. 19. Animation Options [UIView animateWithDuration: 2 delay: 1.2 options: (UIViewAnimationOptions)options animations:^{ ... } completion:^(BOOL finished) { ... } ];
    20. 20. Animation Options• UIViewAnimationOptionAllowUserInteraction • Allows user interaction during an animation (off by default).• UIViewAnimationOptionAutoreverse • Runs the animation back and forth, in a loop• UIViewAnimationOptionBeginFromCurrentState • To avoid jumps between animations, if a second animation starts midway through another animation.• For the full list of all options, search for UIViewAnimationOptions at
    21. 21. Transitions• iOS 4.0+: • transitionFromView:toView:duration:options:co mpletion: • transitionWithView:duration:options:animations: completion• iOS 3.2 and earlier: • setAnimationTransition:forView:cache:
    22. 22. Transitions: CurlUpTo create a curling effect on a view: [UIView transitionWithView: myView duration:2.0 options:UIViewAnimationOptionTransitionCurlUp animations:^{ ... } completion:nil ];
    23. 23. Transitions: CurlUp• Please BEWARE your Code Sense!• Make sure you are using the correct option • DO NOT WANT UIViewAnimationTransitionCurlUp! • This is the old enum used with the (pre-blocks) setAnimationTransition method.• You want: UIViewAnimationOptionTransitionCurl Up
    24. 24. Transitions: CurlUp• If you’d like to transition from an old view to a new view, and want the old view to be removed from the view hierarchy, and the new one added, use: [UIView transitionFromView:myView toView:newView duration:2.0options:UIViewAnimationOptionTransitionCurlUp completion:nil];
    25. 25. Transitions: Flip [UIView transitionWithView:transitionView duration:2.0options:UIViewAnimationOptionTransitionFlipFromLeft animations: nil completion:^(BOOL finished) { ... }];
    26. 26. UIView Resources Apple Docs:• Getting Started with Graphics and Animation• “Animating Views” section of the View Programming Guide for iOSWWDC 2010 Talks & Sample Code:•Building Animation Driven Interfaces• iPlant
    27. 27. CoreAnimation
    28. 28. UIView or CoreAnimation?• Almost always, you should use UIView animations.
    29. 29. When should you use Core Animation? • Use CoreAnimation when you can’t accomplish what you need with UIView animations: • You want to specify motion along a path • UIView gives you linear paths • CoreAnimation lets you specify any path • You want to animate properties that are not animatable in UIView, such as: zPosition, cornerRadius, borderWidth, or borderColor. • You have something very lightweight or short- lived.
    30. 30. When should you use Core Animation? • A great example of a good time to use Core Animation is if want to rotate a UIView. • With UIView animations, we can only rotate around the default anchorPoint of the UIView, which is the middle of the view. • But you can modify that default anchor point through the CALayer of that viewew: [myView.layer.anchorPoint = CGPointMake(0,0); • DEMO: Great visualization of this is in LayerTransforms.m class in Nathan Eror’s CA360 project (
    31. 31. About Core Animation• What is it? A compositing and animation API.• Uses CALayer: • Layers have hierarchies, just like UIViews. • Can add, remove and reorder CALayers in the same way you add/remove/reorder UIViews.• Hardware Accelerated.• Interpolation of animations happens away from the main thread.
    32. 32. Jargon Alert! CompositingCOMPOSITING:“The combining of visual elements from separatesources into single images, often to create the illusionthat all those elements are parts of the same scene.” -- the value of one or moreunknown intermediate pointsbased known start and end points. Image source: Linear_interpolation
    33. 33. Using Core Animation Remember to #import <QuartzCore/QuartzCore.h> and to add the QuartzCore framework to your project.
    34. 34. First Step! Adding Content to yourLayer There are three ways to add content to a CALayer: 1. Set the contents property on the layer using a CGImageRef 2. Use a delegate to provide or draw the content 3. Subclass CALayer and override one of the display methods
    35. 35. Adding Layer Content• Excellent details on the different ways to do this can be found at: • ios/documentation/Cocoa/ Conceptual/CoreAnimation_guide/ Articles/ ProvidingCALayerContent.html
    36. 36. Types of Animation: Implicit vs Explicit • CoreAnimation has both implicit and explicit animations. • Implicit Animations: • Implicit animations occur automatically whenever you modify an animatable layer property. (Sweet!) • Explicit Animations: • Explicit animations are created and then explicitly added to your layer by calling addAnimation:
    37. 37. Implicit Animation• When you set layer properties, the next time the run-loop gets control, it implements a transaction. • That transaction will cause all the layer properties that have changed to animate according to certain defaults. • To override these defaults, you can use CATransaction.• Sample code: BasicAnimation.m
    38. 38. Overriding Implicit Animation Defaults • Let’s say we have the following implicit animation: myLayer.position = CGMakePoint(100, 200); • If we want this to take a bit longer, we can add: [CATransaction setAnimationDuration: 5]; [CATransaction setAnimationTimingFunction: [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut] myLayer.position = CGMakePoint(200,100);
    39. 39. Adding Drop Shadows• @property CGPathRef shadowPath• You specify where the layer is opaque• The compositor takes care of creating the drop shadow, and it does so by caching a shadow bitmap. COMPOSITING: The combining of visual elements from separate sources into single images, often to create the illusion that all those elements are parts of the same scene. Source:• Sample code: ShadowPath.m
    40. 40. Explicit Animations• If you want more control over your animations, use Explicit Animations: • CABasicAnimation • CAKeyframeAnimation • CAAnimationGroup • CATransition
    41. 41. How to Create Explicit Animations • Use keyPath to specify which property you are changing. • @”position.x” • @”transform.scale” • Specify a from value and a to value (make something change). • Add the animation to the layer. [CABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@”opacity”]; ... [myView.layer addAnimation:basic];
    42. 42. CABasicAnimation• Provides simple interpolation for a layer property.• Simple example: BasicAnimation.m
    43. 43. Explicit Animations• Please note: The model value DOES NOT CHANGE! The animation is changing the presentation only.
    44. 44. Explicit Animations: Making it Stick• Because you are not changing the underlying model, the layer will go back to where it was before the animation--unless you also change the model.• Let’s examine BasicAnimation.m from the sample code.• For more on this topic: • • WWDC10’s “Core Animation in
    45. 45. Keyframe Animations• You provide a set of key frames, and Core Animation fills in the gaps.• You can provide the frames through either: • An array of values • Series of points in a CGPathRef
    46. 46. Cubic Keyframe Animations• Interpolation is usually linear -- so interpolating between two points will get you a straight line.• Starting in iOS 4.0, a new calculationMode was added, which calculates the interpolation based on the two points AS WELL AS the points surrounding is--giving a cubic interpolation.• animation.calculationMode = kCAAnimationCubic• Sample code: Keyframe.m
    47. 47. CA360 Project• Nathan Eror (@neror) has an excellent resource on GitHub, CA360, which I used as a base for my sample code• I highly recommend checking it out •
    48. 48. Resources to Learn More • Apple Docs • Getting Started with Graphics and Animation • Core Animation Programming Guide • “Animating Views” section of the View Programming Guide for iOSWWDC 2010 Talks: • Building Animation Driven Interfaces • Core Animation in Practice: Part 1 • Core Animation in Practice: Part IINathan Eror, Freetime Studios • Talk from 360 iDev 2009 • 360 sample code: • FTUtils: