Custom OpenGL UIView transitions
9 November 2010 | | 13 comments
This is my first blog post after a really long time (more than 5 years) :) I hope it was right decision to start again.
Few days ago I found nice class which handles custom OpenGL UIView transitions http://memention.com/blog/2010/02/28/Bells-and-Whistles.html I would probably stick to it and use it, but unfortunately I didn't like the lag before the animation starts and few details (not working on all orientations ...). So I decided to rewrite it from scratch, make it as fast as possible, and maybe a little bit easier (if possible) to use.
I ended up with HMGLTransitions which you can download from here http://github.com/Split82/HMGLTransitions The biggest difference between this and previously mentioned implementations is that I use singleton object which creates OpenGL context only once. This can drastically improve starting lag of animation. Also I don't resample textures and use exact size. Next "big" thing is that all device orientations are supported. Now everything works nice and smooth (except cloth animation on iPhone 3G and lower ;) There are still some things which should be added and there is also space for some performance enhancements, but for now it's enough and can be used without having any problems on iPhone. On iPad there might be performance issues (starting lag) if the view hierarchy which needs to be rendered to the texture is too complicated.
So how do you use this? There are two things that you can actually do:
- Animate transitions between UIViews
- Present and dismiss modal view controllers with custom animation
Both using custom OpenGL based animations.
1. UIView transitions:
You can create UIView transition animation in same manner as creating UIKit transition animations, except transition object must be set first.
FlipTransition *transition = [[[FlipTransition alloc] init] autorelease]; // 1
[[HMGLTransitionManager sharedTransitionManager] setTransition:transition]; // 2
[[HMGLTransitionManager sharedTransitionManager] beginTransition:containerView]; // 3
[view1 removeFromSuperview]; // 4
[containerView addSubview:view2]; // 5
[[HMGLTransitionManager sharedTransitionManager] commitTransition]; // 6
- First new transition object is created. You can implement your own object by subclassing HMGLTransition and implementing all of it's methods. Here FlipTransition object is used.
- Transition object is set
- Transition is initialized with containerView.
- view1 which should be a subview of containerView is removed from display hierarchy
- view2 is added as subview of containerView
- animation starts
2. UIViewController present / dismiss
Presenting and dismissing UIViewController is absolutely straight-forward.
ClothTransition *transition = [[[ClothTransition alloc] init] autorelease]; // 1
[[HMGLTransitionManager sharedTransitionManager] setTransition:transition]; // 2
[[HMGLTransitionManager sharedTransitionManager] presentModalViewController:modalController onViewController:actualController]; // 3
- New transition object is created
- Transition object is set.
- UIViewController is presented. modalController is new UIViewController which we want to present and actualController is UIViewController which is active at the moment (usually it is self)
You can also create your own transitions subclassing HMGLTransition. I will write about it next time.