views:

139

answers:

3

Hi guys,

Is it possible to animate the bounds of the CALayer on iPhone? How to implement that? Thanks!

A: 

Yes, it is possible.

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0f];
[[self view] setBounds:CGRectMake(0.0f, 0.0f, 200.0f, 200.0f];
[UIView commitAnimations];

This will animate a view controller's view from it's current bounds to a bounds of 200 x 200 over 1 second. It won't change the origin--just the size of the bounds rectangle. This is implicit animation by the way. If you want a more complicated animation, look at using CABasicAnimation and animating explicitly.

Matt Long
Thanks!This is what I want to do:Imagine imagesize is 300 x 300. I want the image size to remain the same, but animate the bound area to focus on 100 x 100 specific point of the image.I googled and go through the document and kind of confusing about the frame and bounds.
sooon
If you want to change the position and the bounds you can either animate both using -setCenter: and -setBounds: respectively, or you can animate the frame with a call to -setFrame which will animate both. It's not clear what you are wanting though. If you have a layer that overlays an image, you can animate that layer to focus in as you're describing. In that case though, you'll need to make sure your focusing layer has -setMasksToBounds: set to YES.
Matt Long
A: 

Are you trying to animate the visible part of an image inside a layer, were the layer itself maintains size and position?

That's what CAScrollLayer is made for. Use a CAScrollLayer in place of your current Layer and add image-rendering layerl as a subLayer to the CAScrollLayer. You can then use the transform property of the sublayer to achieve that effect.

tonklon
A: 

I am trying to do something like the Marvels Comics kind of image scrolling. I think I got my answer for now.

By the way, I only use CALayer to do this. I read somewhere that UIKit can do this too. Which would be a better choice?

sooon