Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UIView frame origin and size while animation

I have UIView animation

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:25]; 
myview.frame = CGRectMake(218, 216, myview.frame.size.width * 0.5, myview.frame.size.height * 0.5);
[UIView commitAnimations];

and NSTimer with callback method. The question: is it possible to get current myview.frame size and origin inside timer callback method?
or may be there is another way to trace it?

like image 294
heximal Avatar asked Dec 12 '10 16:12

heximal


3 Answers

I'm pretty sure that it's not possible, because when you change the frame of your view it takes effect immediately. In the background, Core Animation takes care of the animation. So, even if you could grab the frame, it'd give you the final coordinates, not the current coordinates in the midst of an animation.

Access the presentation layer of the property, as pointed out by NWCoder in the comments. See the documentation.

[view.layer.presentationLayer frame]
like image 101
sudo rm -rf Avatar answered Sep 27 '22 21:09

sudo rm -rf


NWCoder is right. I will give you an example in C# since I code in MonoTouch.

   RectangleF start = new RectangleF(0,0,100,100);
   RectangleF end = new RectangleF(100,100,100,100);
   UIView yourView = new UIView(start);

   UIView.Animate (120d, 0d, UIViewAnimationOptions.CurveLinear, delegate {
    yourView.Frame = end;
   }, delegate { });

The code block above will move yourView from 0,0 to 100,100 in 120 seconds. The moment the animation starts, yourView.Frame is already set to (100,100,100,100)...so yourView.Frame.X will equal 100 for the entire 120 seconds.

On the other hand, if you use the first line below any time during the 120 seconds...

   float currentX = yourView.Layer.PresentationLayer.Frame.X
   float currentProp = yourView.Layer.PresentationLayer.Frame.<any other frame property>

...you are in business. You will get the live frame properties while animating.

Works great. I'm using it in my app now.

like image 40
Sheldon Hage Avatar answered Sep 27 '22 21:09

Sheldon Hage


The view's frame is not updated during animation, as you have figured out. You could try myview.layer.frame (just a guess, though I suspect that will not work either).

like image 44
Brian Avatar answered Sep 27 '22 21:09

Brian