Core Animation

Marcus Zarra, Matt Long

Mentioned 9

The definitive full-color reference for Core Animation, Apple's powerful new framework for building feature-rich user interfaces • • Shows Mac developers how to use Core Animation to build state-of-the-art user interfaces that previously required intensely complex OpenGL programming. • Task-based coverage ranges from the absolute basics to advanced optimization and integration - and every feature is illuminates with projects and detailed sample code. • Contains a full chapter on using Core Animation on the iPhone. Apple's Core Animation API offers Mac developers a whole new way to think about user interface design - and the opportunity to quickly and easily integrate advanced UI features ranging from transparent windows to head-up displays. Core Animation contains all the goodies Mac UI designers have been craving - and could previously implement only with highly complex OpenGL code. Core Animation is the definitive reference to this powerful API for every Macintosh and iPhone developer. Leading OSX developers Marcus S. Zarra and Matt Long begin with a complete introduction to Core Animation, offering invaluable insights into when it should be used - and when it shouldn't be. Next, the authors drill down to each specific task developers are likely to perform. Building on the essentials, the authors introduce several advanced techniques, including code optimization and leveraging the GPU to run OpenGL, QuickTime, and Core Animation code in tandem. They also present brand-new coverage of Core Animation programming for the iPhone. Each concept and technique is illuminated with usable code, end-to-end sample projects, functional examples, and advanced project suggestions designed to promote even deeper understanding.

More on Amazon.com

Mentioned in questions and answers.

I have a CABasicAnimation that animating a property of a CALayer e.g. bounds.origin. I want to be able to observe the property changing over time, but haven't really found a method that works 100%.

  1. I tried using KVO (key-value observation) on the presentationLayer's bounds.origin keyPath. The system complains that the object is freed before its observers are freed, leading me to think that the presentation layer is only temporary. Observing presentationLayer.bounds.origin as a keypath doesn't work.

  2. I tried creating a property on another layer and animating that e.g. by declaring the @property and making it @dynamic on that layer. However this new property only gets changed when the presentation layer is accessed (e.g. at the end of the animation), it doesn't seem to update while the animation is running.

  3. I used +needsDisplayForKey on the property in #2, which does trigger updates during the animation, but for these issues:

    • it only works if the CALayer has non-zero frame. Since this layer might be a CAShapeLayer or subclass, it may have a zero frame.
    • it looks like it triggers -setNeedsDisplay for that layer, but since I'm not actually drawing that layer only monitoring the property change, I don't want to cause it to redraw.
  4. I tried scheduling an NSTimer, and within the timer callback sample the presentationLayer. This also works but for these issues:

    • The timer would probably be slightly out of sync with the animation update.
    • Since occasionally the original animation gets pre-empted by another animation, it's difficult to actually get the timer to fire when the animation is running and only when the animation is running.

Any suggestions? All this would be on iPhoneOS 3.0/3.1.

I think you've named all of the possibilities. In fact, I wasn't even aware of #2 and #3 and I wrote the book on Core Animation. ;-)

KVO is not available for these properties. Would be nice if it were, but I believe the reason for this has to do with the overhead it would take. The value would update very frequently and have to call back to any observers.

Anyhow, I've found the NSTimer to be the most reliable approach, but now I'm not sure from what you've said. What makes you think that the timer is out of sync? Why is it difficult get the timer only to fire when the animation is running? Can't you just check for the condition you want in the timer callback and then do nothing if the condition is not met?

Best Regards.

Can anyone here recommend any good books for getting my head around Core animation? I've been through the Apple docs and while I'm sure it's all there, I haven't been able to grok Core Animation yet... Is there an a good example I've missed? or some starting document I've overlooked? If not are there any good books out there on Core Animation... the few hits I've gotten while looking on Amazon don't rate anything too high, mostly MacOSX little iphone. Thanks in advance for any suggestions

In addition to the books suggested by Alex and nevan (of which I'd recommend Marcus Zarra and Matt Long's newer Core Animation book, given its greater quantity of iPhone coverage), I taught a class on iPhone Core Animation recently. The video for that class can found on iTunes U. My class notes, with detailed coverage of Core Animation, can be downloaded here (in VoodooPad format).

I also gave a presentation on Core Animation at the recent iPadDevCamp in Chicago, for which the slides and sample code can be found here.

I'm learning to program for the iPhone. I'm trying to figure out how to do some really cool custom view animations. What should I learn, Quartz or OpenGL ES?

For whoever says OpenGL ES, could I use it to animate my views or is only Quartz suitable for the task? Thanks.

Also any resources / the book or anything that teaches you this stuff in the most painless manner?

By Quartz, you probably mean Core Animation. Quartz is simply the Mac / iPhone 2-D drawing API, where Core Animation handles animation of views and layers.

It is very difficult to use OpenGL for performing custom animations on views, but Core Animation is designed for this purpose. It has been my recommendation to people that if they are thinking about doing 2-D animations, even in games, they should look at Core Animation first and go to OpenGL ES only if Core Animation's performance is unacceptable. Core Animation lets you do a lot with just a little code, and there are now plenty of great references out there on the topic (such as Marcus Zarra and Matt Long's new book).

You may wish to refer to some of these other questions for more:

I have some development experience of CocoaTouch Layer, Core Service Layer iPhone Applications.

Now, I have to migrate for some pure 2d and 3d graphical iPhone Applications.

For that as per my knowledge, I need following frameworks to learn :

1) Core Graphics 2) OpenGL ES 3) Quartz Core

I want to know that what should be the approach to learn this framework quickly.

Or else which framework to learn and which is not, and using which source I should learn this frameworks.

Thanks in Advance

Core Graphics

For this, I'd first recommend reading the Quartz 2D Programming Guide. It's the best set of documentation you'll find out there on the subject. Next, I'd read through some of the questions here tagged core-graphics, quartz-2d, or quartz-graphics. There are some great source code examples in the answers to those questions.

OpenGL ES

There are many "getting started with OpenGL ES" questions here, with links to many resources. These include:

I personally recommend the book "Mobile 3D Graphics: with OpenGL ES and M3G", as well as the "OpenGL ES Programming Guide for iPhone OS". My application Molecules is open source and it uses OpenGL ES. Finally, Jeff LaMarche has a great tutorial series on OpenGL ES.

Core Animation

By Quartz Core, you probably mean Core Animation. Again, I'd start with Apple's Core Animation Programming Guide, then take a look at the code examples you can find here under the various core-animation tagged questions. Beyond that, Bill Dudney's Core Animation for Mac OS X and the iPhone book is a very good introduction, and Marcus Zarra and Matt Long's Core Animation book looks to go into more detail on the subject.

I want to create exactly same as scrolling of images as sonypictures at app stores. Is it possible with scrollview.

screenshot

Please any body help me.How can I create that one.

First of, if you only wanted the behavior of partly showing the left and right posters on a 2D surface like when you are switching tabs in Safari on iPhone. That would be possible to do with a regular scroll view. There are many questions discussing it, like paging like safari, iPad document preview or paging uiscrollview and they in turn link to other related questions and answers. If that is an okay solution for you, then I'm sure that you can find your answer in one of those posts.

If however, it is the 3D cylinder that you are after then you will have to either use a third party framework or you will have to write your own code to get the cylinder effect.

For writing your own code there are two aspects that you need to consider. The first is how the interface will look and the other is how it will feel when the user scrolls in the user face. I haven't used the Sony app so I don't know how it behaves. My guess is that it "pages" i.e. always stops with a movie poster centered, just like the UIScrollView can do for you.

You could base your solution of a UIScrollView with paging turned on to get the nicest scrolling but you could also just add a UISwipeGestureRecognizer to your view and use left and right swipes to switch one poster to the left or right.

If you go for the UIScrollView approach then I suggest that you look at the links above and try working your way from there (and ask new questions when you get stuck).

With the second approach (UISwipeGestureRecognizer) you would have a main view, I will call it "movies view". Its layer would have many "poster layer"-sublayers with one poster each. Depending on the number of posters that you want visible on the screen you could then define a CATransform3D for each position of the posters (center, left and right (and optionally leftEdge, rightEdge or something like that). Then when the UISwipeGestureRecognizer detects a swipe anywhere in the movies view (the entire view) then you would change the transform property on all the layers so that they would animate to their new positions. You would also have transforms that move a layer on and off the screen on the left and right side. By using rotation and perspective (Google "CATransform m34" if your not sure what I mean) you could make it seem like the layers move along a cylinder.

If you search the web or just StackOverflow for "Core Animation CoverFlow" you will find answers that can help you get you started. Just change the rotation so that the off-center layers rotate away from you, make the layers smaller and move then slightly down and you will achieve the "cylinder effect" that the Sony app has.

If you get stuck or just want to learn more about Core Animation there are a lot of great resources out there like these books M. Zarra & M. Long: Core Animation, B. Dudney: Core Animation.

Also, this screencast-series by Bill Dudney (same as the second book) is a fantastic Core Animation resource. And episode 4 even works on a Cover Flow-like application.

I am fairly new to transition and animation methods in Iphone. Can somebody pl. guide me the basics of transition and Animation in Iphone. This is needed frequently in my Apps. Can anybody tell me any link where i can Understand ABC of animation ?

When it comes to Core Animation and UIView animations, you might want to look at Marcus Zarra and Matt Long's new book Core Animation: Simplified Animation Techniques for Mac and iPhone Development. It provides a very thorough walkthrough of Core Animation, from the high level UIView animations to more complex animations involving CALayers and CAAnimations. Bill Dudney also has an excellent Core Animation book.

As far as official documentation, the Core Animation Programming Guide is also well-written.

There are many questions here on Core Animation, tagged with core-animation.

Matt Gallagher has an interesting series of articles entitled "An Asteroids-style game in Core Animation".

Finally, I teach a course on iPhone development, in which I devoted a class to Core Animation and the neat things you can do in it. The course notes are available for download (VoodooPad format), and have quite a bit of detail in them, in addition to links to other resources and sample applications.

I'm looking to create simple animations (fades, windows sliding away, graphs moving or "filling up") in a Cocoa app I'm writing for OS X. I've tried finding information on Core Animation, Core Graphics, NSViewAnimation, and Quartz Composer and how to use these in a Cocoa app, but I'm quickly losing direction. There was also one source saying that Core Animation was no longer supported as of MountainLion. What should be used for basic animations? Is there a specific framework or third-party library that will be useful, or a best-practice method for animating productivity apps?

I apologize for not being more specific, but I'm a bit confused on what exactly to look up.

The simplest thing to do is to modify the properties of your view's layers. To do that, you need to set the flag for your views that says "wants layer" (I work mostly in iOS these days, so I forget exactly what it's called.) When you do that for a view, that view and all it's subviews are backed with CALayer objects.

Changes to animatable properties of layers are animated "implicitly" by default. You can also enclose changes to a layer's properties inside a CATransaction begin/end block to change things like the layer timing and animation curve.

The next step up in complexity is to create CAAnimation objects and add them to your layer. Start with CABasicAnimation. You should be able to find tutorials on the net that show you how to animate layer properties using CABasicAnimations.

The documentation is pretty thin, I'm afraid.

iOS adds a set of view animation methods that make animating views very easy. Sadly, there is no equivalent on Mac OS. You have to deal with animation proxies, which is kind of confusing. There are a couple of Core Animation books you might look into. One is by Marcus Zarra, and the other by Bill Dudney.

EDIT: Another option I didn't mention in my (rather old) post: Mac OS view objects have something called an animation proxy. You can send property changes to a view's animation proxy and the system creates an animation that makes that change.

If you're using auto-layout, beware that changing a view's frame or bounds doesn't work as expected because auto-layout tries to take over size and placement of your view objects. Instead, you want to add outlets to your constraints and animate changes to the settings of the constraints.

I wanted to learn some graphics and thereby gaming coding in iPhone. I have experience in Nav/Tab/View based applications but never worked on OpenGL or Quartz.

Could anyone guide my way to this new exciting journey of graphics. Please suggest tutorials, session videos, example videos... basically anything that could help me understanding the related concepts.

Thanks in advance!

PS: I have a developer account.

I would suggest that you also consider using Cocos2d, Box2d or something similar. Here is a good book.

http://www.amazon.com/Learning-Cocos2D-Hands-On-Building-Chipmunk/dp/0321735625/ref=sr_1_2?ie=UTF8&qid=1341100387&sr=8-2&keywords=cocos2d

Ray Wenderlich also has a game programming starter kit on their website which you could consider buying - http://www.raywenderlich.com/store/space-game-starter-kit

Here are a couple good and free resources:

Those are couple good books and videos. Also make sure to check out Ray Wenderlich's site; it contain's many good, complete tutorials for beginners. Hope this helps!