MPFlipViewController: a page-flipping container controller

I put a project called MPFlipViewController up on GitHub. It’s a page-flipping container view controller that allows the user to flip through a series of view controllers as if they were pages in a book. It is based on the MPFlipTransition class I already have on GitHub, but instead of being just a transition between 2 views, it is a full-fledged container view controller that supports panning and swiping between pages (child view controllers). It follows the Containment API introduced in iOS 5, so it behaves as a proper container view controller similar to the system container controllers (e.g. UINavigationController, UITabBarController and UIPageViewController).


Xcode 4.3
iOS 5


The API is based on the API for UIPageViewController (since they fulfill an almost identical role). There is a data source protocol that you implement to specify the previous and next pages, and a delegate protocol that you implement in order to receive feedback on whether or not a page-turn operation completed and also to optionally specify the new orientation in the event that device orientation changes (i.e. user rotates the device).


To create a flip controller use the initWithOrientation: method and pass in the desired orientation (horizontal or vertical):

- (id)initWithOrientation:(MPFlipViewControllerOrientation)orientation;

To set the content use the setViewController:direction:animated:completion: method where direction indicates whether the animation should be a page flip forward or backward.

- (void)setViewController:(UIViewController *)viewController 
               completion:(void (^)(BOOL finished))completion;

To enable touch gestures (panning and swiping between pages) implement the MPFlipViewControllerDataSource delegate to provide the previous and next pages (if any). Return nil for either method to indicate the user is already on the first or last page.

- (UIViewController *)flipViewController:
          (MPFlipViewController *)flipViewController
          (UIViewController *)viewController;

- (UIViewController *)flipViewController:
          (MPFlipViewController *)flipViewController
          (UIViewController *)viewController;

To be notified of whether a page turn animation completed or not, set the MPFlipViewControllerDelegate and implement the optional flipViewController:didFinishAnimating:previousViewController:transitionCompleted: method. This method is only called if the page turn was gesture-driven (i.e. in response to a pan or swipe), and not programmatic (i.e. in response to a call to setViewController:direction:animated:completion:).

- (void)flipViewController:(MPFlipViewController *)flipViewController 
    previousViewController:(UIViewController *)previousViewController 

To change the orientation of the flip controller when device orientation changes, set the MPFlipViewControllerDelegate and implement the optional flipViewController:orientationForInterfaceOrientation: method and return the desired orientation.

- (MPFlipViewControllerOrientation)flipViewController:
                       (MPFlipViewController *)flipViewController 

Demo Project

The GitHub project includes a sample project that demonstrates the use of the control and its API.

Don’t rename your .xcdatamodeld file

Ok, you can rename it but be prepared for weird bugs with Xcode 4.3 (and possibly earlier versions).  In my case I created a new Core Data model file and added it to an existing project.  Then I renamed the model file before attempting to build the project.  Yet even with a clean build and install (deleting the app from the device first), NSManagedObjectModel -initWithContentsOfURL returned nil even though my NSURL pointer was non-nil.

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"ModelName"
__managedObjectModel = [[NSManagedObjectModel alloc] 

Short answer: quit and reopen Xcode to fix the problem.

(Solution via Stack Overflow)

On the importance of setting contentsScale in CATextLayer

The retina display iPhone has been out for 2 years now, so I probably should have run into this before, but as I use CATextLayer infrequently I had not.  Anyway, if you don’t manually set the contentsScale property of your CATextLayer to the correct value, on retina devices you’ll end up with blocky pixel-doubled text like this:

However, if you call:

if ([layer respondsToSelector:@selector(setContentsScale:)])
    [layer setContentsScale:[[UIScreen mainScreen] scale]];

then you will get properly rendered text like so:

(Solution via StackOverflow.)

iOSDevCampDC 2012

I am pleased to announce that I will be speaking at iOSDevCampDC in Reston, VA on August 11.  This is a single track, single day mini-conference (for a mini price, just $50) that brings together a lot of DC-area developers.  It’s on a Saturday, so work conflicts are minimal.  Ticket sales end this Saturday (July 14), so if you’d like to go, act soon.

Update: registration has been extended until July 24.

I will be doing my “Enter The Matrix” presentation on matrix transformations, but I’ll probably fine-tune it to the more advanced audience I expect to quickly review the basics and then spend more time examining flipping and folding animations (and related ephemera such as anti-aliasing, avoiding off-screen render passes, shadows, rendering retina images, etc.)

So if you’re in the area (or will be), I hope to see you there!  (Seriously, for $50 you can’t beat the price for 6 technical sessions plus the opportunity to network with a cadre of local devs.)

CocoaConf DC wrap-up

Last weekend I had a great time at CocoaConf DC on my home turf of northern Virginia.  In many ways CocoaConf is the antithesis of WWDC.  Capped at 100 attendees, it’s small where WWDC is enormous, and intimate where WWDC can be impersonal.  It’s a conference where you can meet (and spend time chatting with) every speaker and every attendee, if you put your mind to it.  And really, although the sessions are the ostensible reason to attend conferences (the way we justify them to our bosses or to ourselves), for me at least it’s the personal connections you make that are the real value.  As developers we tend to spend most of our time isolated in our cubicles (and in some cases our homes), so it’s doubly important to take the time to reach out to the like-minded individuals we encounter at conferences.  CocoaConf is a great venue for that.  And with 3 tracks of 30 sessions over 2 days (preceded by an optional full-day intensive tutorial), there’s plenty of knowledge to be gained as well.  A common theme I heard throughout last weekend was the “which of these sessions should I attend next?  They all sound great” dilemma (trilemma?).

This time I presented two talks.  I debuted a talk on custom container view controllers (using the iOS 5 containment API), which in addition to containment covered the iterative process of designing and refining the UI and API for a page-flipping control (demo app shown above).  The slides are available here and the code (open source and attribution-only licensed) is up on GitHub.

My other talk, “Enter The Matrix”, I gave for the second time.  It’s about using matrix transformations in Quartz drawing, UIKit animations, and CoreAnimation (with obligatory, gratuitous references to the Matrix movies).  Since Chicago in March, I’ve significantly improved the accompanying sample app, completely revised the slides for fold and flip animations, and introduced a section on skew and perspective.  The slides are available here (latest version here) and the code is on GitHub.

I think one new talk is the most I can manage per conference.  A great deal of work goes into each presentation (code, slides, and speech).  I like this picture of Saul Mora, Scott McAlister and me all preparing for our talks on the first day of the conference.  We work on our presentations up until the last minute not because we’re ill-prepared, but rather because we care.

One little touch that I really appreciated as a speaker: a MacBook power adapter installed on the presenter’s table in each of the rooms.  Sometimes, it’s the attention to little details that really stand out.

I hear that CocoaConf may be back in DC next March.  If so, I hope I can be a part of it again, either as speaker or attendee.