iOS Book Giveaway: iOS Core Animation, Advanced Techniques

And the winner is … James Cambpell

Congratulations James! Contact me by email to claim your prize.

Next week will be giving away a copy of Effective Objective-C 2.0, by Matt Galloway.

This week we are giving away a copy iOS Core Animation: Advanced Techniques, written by Nick Lockwood.

This book provides an in-depth look at the Core Animation framework, covering a wide range of topics from layers to physics to visual effects.

I’m still working through the book, however, I will say that I’m impressed at the extent of information provided in just the first few chapters.

Core Animation and the related topics are not trivial. Nonetheless, I found this book to provide a balance of technical information along with good examples. Experienced iOS dev’s should find this to be a good resource to quickly get up to speed.

Here’s a little background on the book in Nick’s words:

When Pearson approached me to write a book about iOS, I immediately thought of Core Animation because it’s something that I use a lot, but which seems to be poorly understood by most of the iOS development community. I did some research to see if there were any other books on the subject, but whilst there were several general iOS development books that touched on Core Animation, the only book I found that was devoted to the subject was Bill Dudney’s Core Animation for Mac OS X and the iPhone, which was published in 2008 and is now out of print.

I knew that I’d learn a lot by writing a book, but I didn’t guess at just how little I really knew about Core Animation before I sat down to try to explain it. I learned many things that I’d never guessed at from skimming the documentation – in particular the existence and role of the render server, and the various layer trees that exist below the surface that you normally interact with.

Core Animation is a phenomenally complex and powerful tool, but we mostly either fail to make full use of it, or misuse it because we don’t understand what’s really going on behind the scenes. In the course of writing iOS Core Animation: Advanced Techniques, I learned me how to harness Core Animation to build pretty much any kind of user interface I can imagine, and by reading it, you will too!

Core Animation Questions?

Nick will be here during the week. Don’t miss the opportunity to chat with someone who’s been fully immersed in Core Animation.

Enter the Giveaway

Here’s how to enter the giveaway for a copy of iOS Core Animation: Advanced Techniques:

  • Read the iOS Developer Tips Giveaway Rules.
  • Post a relevant question about Core Animation, share your experience with Core Animation or contribute to an ongoing discussion (posting “hope I win” or something similar does not qualify).
  • The deadline to enter the giveaway will be Friday, 2/28/14 at 12:00PM EST.

The winner be selected sometime on Friday 2/28/14 via random number selection from eligible entries. The winner will be announced here.

Many thanks to Stephane Nakib at Pearson for helping to arrange this giveaway!

33 Comments

  1. This is great. I haven’t had much experience dealing with core animation but it is something I’d like to improve in. Might be looking into this as a potential resource for me in the future.

    • The view.layer.mask property suggested in the first answer sounds like the right approach for this particular problem. Use of layer masks is covered in detail in Chapter 4: Visual Effects :-)

  2. I am a web developer who learns iOS programming from online resources like raywenderlich, iTunes U, codeschool and so on. This book would help me to learn more advanced stuff on Core Animation.

    • Definitely. It’s aimed at users who are already familiar with iOS and UIKit though, so I recommend learning the basics first.

  3. What is the best way to implement 3D effects, such as paper folding ?

    • If you use view.layer.transform you can rotate and translate views in 3 dimensions. By setting transform.m34 = -1.0/500 you can set the camera perspective. This is explained in detail in Chapter 5: Transforms

  4. How do you maintain z order of animation cleanly.

    • Views will always be drawn in the order that they appear in their parent view’s subviews array. You can override this by manipulating view.layer.zPosition, but this only affects the order in which views are drawn, not the order in which touch events are processed.

  5. What’s a good rule of thumb for knowing when to add an animation and when not to? For example, if I’m toggling something like a checkbox, it seems like drawing a checkmark in the box using animation might be overkill. Or should I do it, but just really really fast?

    • In the case of a checkbox I probably wouldn’t bother with anything complicated, but you could experiment with adding a crossfade animation, which is super-easy to do using Core Animation. Just use this code:

      //set up transition
      CATransition *transition = [CATransition animation];
      [checkboxView.layer addAnimation:transition forKey:nil];

      //now set the checkboxView ticked state and the transition will crossfade it.

      This trick works when setting any property of any view, even if you can’t normally animate it.

  6. What is the best way to have particles animation as a layer with transparency (not opaque background) and be able to pause and capture that view? (Then the purpose combine that view with another image as an overlay)

    I looked around and found out that I could use OpenGL base on a tutorial on Ray Wenderlich website. So I did use it OpenGL for my app Snow Me (http://getsnow.me) but the problem I face is the phone gets really hot when there are a lot of snow, which makes me think that it’s not the best way to implement such feature.

    • You could try using CAEmitterLayer, which is a particle engine built into Core Animation. Alternatively, if you’re OK with going iOS7-only, there’s an awesome particle engine in SpriteKit, with a particle designer tool built directly into Xcode.

  7. I don’t have any experience with CoreAnimation, yet. But my wife is always having new ideas for iOS games, so I had planned to get into that suject one day. Maybe now is the right time?

    • To be honest, if you’re predominately interested in writing games, you’d probably be better off looking at SpriteKit instead of Core Animation. You might like my Floaty Balloon tutorial on iosdevelopertips from last week though :-)

  8. The great thing about CoreAnimation is that for most animations, the SDK provides a simple way to achieve the effect. In my very first iOS app back in 2009 (we called them iPhone apps back then, as there was no iPad yet!) I wanted to achieve a game board rotation. With no animation experience, let alone very little iPhone or Max OS skills, I thought for sure this would be hard to do. Imagine my delight when it turned out I could achieve my goal with literally 2 lines of code. (Two transforms.) This is great stuff, and I am glad to see more books and resources appearing devoted to individual topics like this.

  9. I would like to learn more about animation effects and layers using touch events for unique presentations.

  10. I have not used Core Animation to its fullest, I have only played with short simple animations concerning the alpha of a view. I would really like to learn more on this topic, to be able to create beautiful(ant not overly-intrusive) animations

  11. On Mac OS X, I want to implement delayed animations that are cancelable. That is I want to queue up an animation to run some time in the future, but I need the ability to change my mind if the animation has yet to start (visible) execution. How can this be accomplished using CA?

    • When using CATransactions to set up implicit animations, you can set a completion block to fire after the animation finishes. If you use the completion block to trigger your next animation, you can have a boolean flag that you check to see if the animations have been cancelled.

      Alternatively, if you are using explicit CAPropertyAnimations, you can combine them into a CAAnimationGroup and then cancel them with a single command.

  12. I’m new with iOS, and there are a lot of questions that i want to ask. Does this book require any knowledge or experience of iOS? When is the right time to read this book?

    • The book is aimed at intermediate to advanced developers. I recommend buying a beginners book on iOS first, and then trying my book once you have mastered the basics.

  13. How long on average does it take to work through the book?

    • I have no idea I’m afraid, I only wrote it – I haven’t read it ;-)

      There are code samples, but no exercises. You can probably read through it in a couple of days, but if you run all the code samples and study how they work it will take longer.

  14. When using animateWithDuration, the completion block will be called immediately if no property values are changed in the animations block. However, this can cause unexpected results if you are attempting to chain sequences of animations which rely on the duration of the previous animation for timing.

    Is there any way to cause animateWithDuration to respect its delay and duration even if no changes occur?

    My current solution is to check before triggering the animation if any properties are actually changing, and if not, use the GCD dispatch_after function to trigger subsequent animations after a time period which is the delay+duration of the animation. This is however not ideal as dispatch_after may not be time accurate, and leads to a slightly messy code path.

    • I hadn’t noticed that before, and TBH it sounds like a bug that you should probably report to Apple. Your GCD solution seems like the only workable approach if that is the case.

    • I’m not sure it’s a bug, probably more an optimisation on the rendering engine. If there are no changes to be made, the animation is by definition finished,and so the completion block executes. There’s no point in tweening value if nothing is changing.

      Unfortunately it can lead to unexpected timings if you’re assuming the duration will always be honoured before the completion block executes, and this is not documented in the UIView reference.

  15. I’ve never really looked into Core Animation much before, but it would be great to add a little polish to my app with page turns and smooth transitions when the view is updated.

    • That’s a tricky one. Autolayout only applies to the frame of the view/layer, not the transform. The problem is that the transform also affects the frame, so if you scale a view it’s frame will increase or decrease, which will then interfere with auto layout.

      It’s safe to apply translations to views that have constraints, because translations don’t affect the frame, but I’d advise against rotating or scaling them. If you want to apply these kinds of transform, put the view you want to transform inside another empty view of the same size, so that the outer view has the constraints, and the inner view has the transform.

      That way, you avoid the interference.

  16. When is it good to you blocks when dealing with animations?

  17. How should I deal with orientation when using storyboards with auto layout turned off. I know I could create a view and use self.bounds but is this the recommended approach for compatibility with retina and none retina?

    • Retina/non retina doesn’t affect the bounds because bounds are measured in points, not pixels, so they are resolution independent.

Comments are closed.