views:

442

answers:

3

I have a single UIView for drawing any one of a set of items. That is, my one UIView subclass can be told to draw a square, circle, or triangle for example.

I'd like to have a transition to flip that view so that the UIView draws a different shape at the half way point, so it looks like I'm transitioning to a different view, but really I'm just redrawing the single view at the half way point of the transition.

Is there a way to do this, without resorting to using two different UIViews and swapping them out in the animation block? It would be a bit clumsy for me to rejig what I have to use the swap mechanism.

Ideally all I need is some callback or notification that the animation is at the half way point, and then I can redraw the view with the new shape then.

Thanks for any help!

A: 

you can try to do smth like this. For example, your animationDuration property is set to 2.0f. so, the half way point is 1 second, so you can use

[self performSelector:@selector(yourCallbackMethod) withObject:nil afterDelay:1.0f];

Morion
I also had this thought, but quickly discounted it as clumsy when using short duration times - the redrawing of the image has to take place perfectly on the halfway point to look acceptable, but I think it would not be possible to guarantee getting called at precisely this point using performSelector (you're only guaranteed being called __after__ the delay, not __at__ it).But thanks for your input!
Nex
A: 

The only way I know of that you could do this is to re-create the animation using Core Animation and then monitor the animation in the presentationLayer using a timer. There is no KVO available in Core Animation Layers so you have to monitor it explicitly.

In other words, it's probably not worth it. I suggest you think of a different way to solve your problem.

Matt Long
Thanks, that's what I thought - if there was a simple callback from CA at precisely the mid-point that would have been great. Working around it with my own timers is too clumsy, as you rightly also mention. I'll just implement a view switching mechanism. It means I'll instantiate an extra view every time I want the animation, and will have to deal with that, which is a shame, but it's not the end of the world.Thanks.
Nex
+1  A: 

Even if you did know the midpoint of your flip animation, I don't believe redrawing the content of the view mid-animation would do anything. I think that the contents of the view's layer are cached at the beginning of the animation and don't change as the animation proceeds. Also, your view would end up inverted, left to right, at the end of the animation if you didn't have another view behind it.

If you wish to do this as a custom animation, you could break this into two halves using something like Mike Lee's Lemur Flip implementation that I describe in this answer. After the first half of the animation, you could redraw the view's content and complete the animation, ending up back where you started.

However, to me it seems more clumsy to not switch views in response to the transition. It certainly will take a lot more code to do.

Brad Larson
Hi Brad, thanks for that insight. It's much appreciated.I think you're absolutely right, I was hoping for a simple shortcut so I wouldn't have to instantiate another view when it would be nice just to use the view I have. However any workaround, as you point out, is greatly more complex than just building in a mechanism to instantiate and swap in a new view as the docs suggest.Thanks for your time though!
Nex