Wednesday 21 October 2009

MonoTouch flip-flop with UIView animation

MonospaceThanks to Beginning iPhone Development's Objective-C examples the Monospace app now does a flip-flop between two views (the map and a new list of map locations to 'pan to'). The source code is available for download (you'll also notice the UI has been made "black"... I just like the way it looks).

Monospace22.zip (188Kb)




The view is driven by the MapFlipViewController (which is what we add to the TabViewController). It is responsible for creating/displaying the two 'flippable' views using UIView animation features.

The two views - MapViewController & MapLocationViewController - have a constructor which accepts a reference to the 'flipper' so they can call back to it's Flip() method, which looks something like this...
UIView.BeginAnimations("Flipper");
UIView.SetAnimationDuration(1.25);
UIView.SetAnimationCurve(UIViewAnimationCurve.EaseInOut);
if (mapView.View.Superview == null)
{
Console.WriteLine("to map");
UIView.SetAnimationTransition
(UIViewAnimationTransition.FlipFromRight, this.View, true);
locationView.ViewWillAppear(true);
mapView.ViewWillDisappear(true);

locationView.View.RemoveFromSuperview();
this.View.AddSubview(mapView.View);

mapView.ViewDidDisappear(true);
locationView.ViewDidAppear(true);
}
else
{
Console.WriteLine("to list");
UIView.SetAnimationTransition
(UIViewAnimationTransition.FlipFromLeft, this.View, true);
mapView.ViewWillAppear(true);
locationView.ViewWillDisappear(true);

mapView.View.RemoveFromSuperview();
this.View.AddSubview(locationView.View);

locationView.ViewDidDisappear(true);
mapView.ViewDidAppear(true);
}
UIView.CommitAnimations();
There is other code to actually set the map to a specific location - but that's actually pretty simple :)
mapView.SetLocation(toLocation);

6 comments:

  1. Thanks Craig your experimentations with MonoTouch is surely helping me to grasp a lot more of the Cocoa Touch concepts underneath MonoTouch.

    ReplyDelete
  2. Craig,

    Thanks for posting the info. It looks like the links aren't working, however, to the source?

    Steve

    ReplyDelete
  3. Hey Steve, I'll check out the server where the code is hosted... Should probably put on googlecode or git at some point.
    Thanks for reading:)

    ReplyDelete
  4. UPDATE: code download link should be working again.

    Monospace22.zip (188Kb)

    ReplyDelete
  5. Craig, I'm using similar code myself. One thing I have found is that this seems to break the rotaition handling. The two controllers that are flipped no longer rotate correctly. What I've had to do is add an observer on the flipcontroller for OrientationDidChangeNotification and within that call controller1.View.SetNeedsLayout() and controller2.View.SetNeedsLayout(). But then any controllers those controllers invoke don't rotate properly.

    If I don't use the flip controllers but just push them directly, they rotate fine so definitely seems to be the flipcontroller causing the problem.

    I am wondering if you had same issue. I tried to run your app but its failing when I start it up.

    ReplyDelete
  6. Hi,It looks like the links aren't working.

    ReplyDelete

Note: only a member of this blog may post a comment.