user700774 user700774 - 1 month ago 13
iOS Question

UIView animation determine center during animation

I'm using UIView's

+ animateWithDuration:delay:options:animations:completion:
method to move my view along a line over the course of a few seconds or so.

I would like to determine, at an arbitrary time during this animation, where the UIView is along with path it is moving. However when I do this and try and access
center
or
frame
, I find that the location is already set to it's final destination.

The reason I'm doing this is that I've got an NSTimer firing every 0.1 seconds (or thereabouts) which will update the parent view to show a line where the UIView has previously been. I want to be able to keep this updated as the UIView moves.

Any ideas on how to do this? The only similar question I found was http://forums.macrumors.com/showthread.php?t=1056105, but there is no resolution shown.

Alternatively...are there any better ways to do this?

Thanks!

Answer

UIViews are backed by CALayers, and it is the layer that does the actual animation. CALayer exposes a property presentationLayer which, when accessed, returns a copy of the CALayer that represents, as closely as it can, the current state visible to the user. By asking for the view.layer.presentationLayer.frame you can get the frame of the layer as visible to the user right now, and this corresponds to the frame of the UIView.