有什么方法可以使用Core Animation来创建卷曲或剥离动画?

时间:2010-07-07 12:49:46

标签: iphone ipad core-animation

我正在试图弄清楚如何使用Core Animation在我的iPad应用程序中创建a curl or peel effect on an image,但在我学习API时,我只了解移动,缩放和旋转2D的不同方法图像(包括3D变换)。我想在两侧都有图像的小的单个图层(即精灵)上实现这种效果,因此当你剥离图层时,你会在背面曝光图像。我还需要控制层剥离的位置和角度。我对数学很恐怖,并且希望比我聪明的人能帮助我,只要我至少理解我提到的那些基本变换。

非常感谢你的智慧!

2 个答案:

答案 0 :(得分:1)

轻松创建它的唯一方法是使用UIView的类方法,如下所示:

此代码位于MyViewController.h文件中:

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

此代码位于MyViewController.m文件中:

 @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

由于在此示例中,以编程方式创建了UIViewController,因此使用方法“loadView”来创建由视图控制器管理的视图以及此视图将管理的子视图。我希望这有帮助。欢呼声。

答案 1 :(得分:0)

你可以在大多数情况下跳过数学运算,让api处理所有事情。试试这段代码:

- (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];
}