user
user

Reputation: 3456

How can I return a view to its original state after animating it?

I have a rectangular view with two labels (one static, one dynamic) set from a xib. When the user touches the view, I have it contract and hide the labels (using UIView's -animateWithDuration: animations:).

Is there any way I can save the context of just the view, and return to it later?

I know there is UIGraphicsGetCurrentContext but the documentation just says: Returns graphics context which I am guessing is the entire view. (What horrible documentation!)

It seems this is possible and common with CALayers, but this is just a UIView method and I'm not sure if it uses CALayers in its implementation.

Upvotes: 0

Views: 152

Answers (2)

lxt
lxt

Reputation: 31304

So to answer a few of your questions:

Firstly, UIView is at its core a wrapper for a CALayer. You can get access to the underlying CALayer through the view's layer property. If you're doing a lot of animation work you'll probably find this very useful at some point. A 'graphics context', in the iOS sense of the word, is basically a place where information about the current drawing state is stored.

The convenience methods that UIView provides for animation (animateWithDuration, etc) automatically set the frame, bounds, etc of the view to their newly animated values once the animation has completed. You'll therefore want to keep hold of whatever values you are changing so you can animate back to them when required. Note that this might not just be the frame - if you're changing the alpha values you'll want to hang on to them as well, etc etc.

There are other options that you can try, but they involve using the slightly lower-level Core Animation APIs. This existing StackOverflow question has a really good summary: UIView animations with Autoreverse

Upvotes: 1

EricS
EricS

Reputation: 9768

No, you can't easily save and restore the entire view and subview contents. UIGraphicsGetCurrentContext can be used to take a picture of a UIView, but not to save the subview locations.

Instead keep two sets of frames for every subview and then switch between them by setting mySubview.frame.

Upvotes: 2

Related Questions