views:

78

answers:

2

I'm trying to figure out how I could create a curl or peel effect on an image in my iPad app using Core Animation, but as I'm learning the API, I only understand different ways to move, scale, and rotate the 2D image (including the 3D transformations). I'd like to accomplish this effect on small, individual layers (i.e. sprites) that have images on both sides, so as you peel back the layer, you expose the image on the backside. I also need to have control over the position and angle at which the layer peels. I'm horrible at math and was hoping someone smarter than me could help me out so long as I at least understood those basic transformations that I mentioned.

Thanks so much in advance for wisdom!

A: 

You can skip the math in most situations and let the api handle everything. Try this code:

- (IBAction)showPeelBack:(id)sender{
    FooController *controller = [[CreditsController alloc] initWithNibName:@"Foo" bundle:nil];
    // setup the stuff you want to display
    [controller setViewController: self];

    controller.modalPresentationStyle = UIModalPresentationFullScreen;
    controller.modalTransitionStyle = UIModalTransitionStylePartialCurl;

    // note: I'm using a nav controller here
    [[self navigationController] presentModalViewController:controller animated:YES];
    [controller release];
}
Brandon
Thanks, Brandon! I'm not sure the UIModalTransitionStylePartialCurl is the functionality I'm looking for since it doesn't really allow me to control the animation fully like the other transformations allow me to do (i.e i can't curl the page from any angle, nor can i hide the curl when i don't want it). Not to mention, this only works on views that are fullscreen and I'm trying to accomplish this effect on individual sprites. Your thoughts?
BeachRunnerJoe
Oops, it doesn't look like the modal view suits your needs. You're right, that UIModalTransitionStylePartialCurl does toss an exception on anything but a UIModalPresentationFullScreen presentation style. I totally didn't notice that.I don't know how to do the peel transition any other way. Sorry.
Brandon
+1  A: 

The only way in which to easily create it is through the use of UIView's class methods as follows:

This code goes within the MyViewController.h file:

 @interface MyViewController : UIViewController
    {
       UIImageView* imageView1;
       UIImageView* imageView2;
    }
  @end

This code goes within the MyViewController.m file:

 @implementation MyViewController

    - (void) dealloc
    {
       [imageView2 release];
       [imageView1 release];

       [super dealloc];
    }

    - (void) loadView
    {
       self.view = [[UIView alloc] initWithFrame : CGRectMake (0, 0, 768, 1004)];

       UIImage* image1 = [[UIImage alloc] initWithContentsOfFile : @"image1.png"];
       UIImage* image2 = [[UIImage alloc] initWithContentsOfFile : @"image2.png"];

       imageView1 = [[UIImageView alloc] initWithImage : image1];
       imageView2 = [[UIImageView alloc] initWithImage : image2];

       [imageView1 setFrame : CGRectMake (0, 0, 768, 1004)];
       [imageView1 setFrame : CGRectMake (0, 0, 768, 1004)];

       [self.view addSubview : imageView1];

       [image2 release];
       [image1 release];
    }

    - (void) viewDidUnload
    {
       imageView2 = nil;
       imageView1 = nil;

       [super viewDidUnload];
    }

    - (void) touchesBegan : (NSSet*) touches withEvent : (UIEvent*) event
    {
       [UIView beginAnimations : nil context : nil];
       [UIView setAnimationCurve : UIViewAnimationCurveLinear];
       [UIView setAnimationDuration : 0.25];

       if (imageView1.superview != nil)
       {
          [UIView setAnimationTransition : UIViewAnimationTransitionCurlUp forView : self.view cache : YES];
          [imageView1 removeFromSuperview];
          [self.view addSubview : imageView2];
       }
       else
       {
          [UIView setAnimationTransition : UIViewAnimationTransitionCurlDown forView : self.view cache : YES];
          [imageView2 removeFromSuperview];
          [self.view addSubview : imageView1];
       }

       [UIView commitAnimations];
    }
 @end

Since, in this example, the UIViewController is being created programmatically, the method "loadView" was used in order to create the view managed by the view controller and the subviews which this view will manage. I hope this helps. Cheers.